查詢數(shù)據(jù)排名情況SQL
1/準(zhǔn)備測(cè)試數(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
相關(guān)文章
SQL?Server?2008?R2安裝教程及錯(cuò)誤解決方案詳細(xì)教程
這篇文章主要介紹了安裝SQL?Server?2008?R2的步驟,并列舉了幾個(gè)常見的錯(cuò)誤及其解決方法,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2025-02-02
解析關(guān)于SQL語(yǔ)句Count的一點(diǎn)細(xì)節(jié)
本篇文章是對(duì)關(guān)于SQL語(yǔ)句Count的一點(diǎn)細(xì)節(jié)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
MSSQL中進(jìn)行SQL除法運(yùn)算結(jié)果為小數(shù)卻顯示0的解決方法
這篇文章主要介紹了MSSQL中進(jìn)行SQL除法運(yùn)算結(jié)果為小數(shù)卻顯示0的解決方法,需要的朋友可以參考下2015-10-10
SQL Server中的排名函數(shù)與分析函數(shù)詳解
本文詳細(xì)講解了SQL Server中的排名函數(shù)與分析函數(shù),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
省市縣三級(jí)聯(lián)動(dòng)的SQL語(yǔ)句
這篇文章主要介紹了省市縣三級(jí)聯(lián)動(dòng)的SQL語(yǔ)句,包括建表及插入數(shù)據(jù),需要的朋友可以參考下2014-04-04
SQL SERVER 2012數(shù)據(jù)庫(kù)自動(dòng)備份的方法
這篇文章主要為大家詳細(xì)介紹了SQL SERVER 2012數(shù)據(jù)庫(kù)自動(dòng)備份的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
SQL Server誤區(qū)30日談 第29天 有關(guān)堆碎片的誤區(qū)
對(duì)堆建聚集索引再DROP在我看來(lái)是除了收縮數(shù)據(jù)庫(kù)之外最2的事了2013-01-01
sql分組后二次匯總(處理表重復(fù)記錄查詢和刪除)的實(shí)現(xiàn)方法
這篇文章主要介紹了sql分組后二次匯總的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-02-02

