查詢數(shù)據(jù)排名情況SQL
1/準備測試數(shù)據(jù)
---------------------------------------------------------------------------------
create table t1(
c1 integer,
c2 integer,
c3 integer
);
insert into t1 values(1,2,3)
insert into t1 values(1,8,4)
insert into t1 values(1,4,4)
insert into t1 values(1,4,5)
insert into t1 values(1,5,5)
insert into t1 values(2,2,3)
insert into t1 values(2,8,4)
insert into t1 values(2,4,4)
insert into t1 values(2,4,5)
insert into t1 values(2,5,5)
2/查看排名
---------------------------------------------------------------------------------
A/單記錄排名
select c1,c3,
(select count( c3)+1 from t1 a where a.c3>b.c3
and a.c1=b.c1 and a.c1 =1
) order_num
from t1 b
where c1 =1
order by c1,c3
c1 c3 order_num
----------- ----------- ----------------------
1 3 5
1 4 3
1 4 3
1 5 1
1 5 1
B/多記錄排名
select c1,c2,c3,
(select count( c3)+1 from t1 a where a.c3>b.c3
and a.c1=b.c1
) order_num
from t1 b
order by c1,c3
c1 c2 c3 order_num
----------- ----------- ----------- ----------------------
1 2 3 5
1 8 4 3
1 4 4 3
1 4 5 1
1 5 5 1
2 2 3 5
2 8 4 3
2 4 4 3
2 4 5 1
2 5 5 1
相關文章
SQL Server 2016 無域群集配置 AlwaysON 可用性組圖文教程
這篇文章主要介紹了SQL Server 2016 無域群集配置 AlwaysON 可用性組圖文教程,需要的朋友可以參考下2017-04-04sql清空表數(shù)據(jù)后重新添加數(shù)據(jù)存儲過程的示例
這篇文章主要介紹了sql清空表數(shù)據(jù)后重新添加數(shù)據(jù)存儲過程的示例,需要的朋友可以參考下2014-04-04SQL Server誤區(qū)30日談 第19天 Truncate表的操作不會被記錄到日志
這個誤區(qū)也同樣流傳已久,我想是時候通過一些Demo進行揭穿了2013-01-01如何創(chuàng)建支持FILESTREAM的數(shù)據(jù)庫示例探討
FILESTREAM使用一種特殊類型的文件組,因此在創(chuàng)建數(shù)據(jù)庫時,必須至少為一個文件組指定 CONTAINS FILESTREAM 子句接下來為你詳細介紹下如何創(chuàng)建支持 FILESTREAM 的數(shù)據(jù)庫2013-03-03SQLServer 觸發(fā)器 數(shù)據(jù)庫進行數(shù)據(jù)備份
首先,你需要建立測試數(shù)據(jù)表,一個用于插入數(shù)據(jù):test3,另外一個作為備份:test3_bak2009-07-07獲取SQL Server數(shù)據(jù)庫元數(shù)據(jù)的幾種方法
這篇文章主要介紹了獲取SQL Server數(shù)據(jù)庫元數(shù)據(jù)的幾種方法 ,需要的朋友可以參考下2015-08-08Linux安裝ODBC連接SQLServer數(shù)據(jù)庫的步驟
這篇文章主要介紹了Linux安裝ODBC連接SQLServer數(shù)據(jù)庫?,本文分步驟給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04