當前位置:才華齋>設計>網頁設計>

Sql Server、Access資料排名的實現方法

網頁設計 閱讀(2.61W)

但是,在SQL SERVER 2005 之前,SQL SERVER 2000 並沒有提供這個直接的.函式供我們使用,同樣 ACCESS 也是如此。

Sql Server、Access資料排名的實現方法

下面我們分2種情況,來寫出資料排名的實現過程。測試資料如下:

Access

複製程式碼 程式碼如下:

select name, score, (select iif(isnull(sum(1)), 1, sum(1) + 1) from score_rank where score > e) as rank from score_rank a order by score desc

sqlserver

複製程式碼 程式碼如下:

select name, score, (select ISNULL(sum(1),0) + 1 from score_rank where score > e) as rank from score_rank a order by score desc

對於 SQL SERVER 2005 及更高版本

複製程式碼 程式碼如下:

SELECT name, score, RANK() OVER (ORDER BY score DESC) AS [rank], DENSE_RANK() OVER (ORDER BY score DESC) AS [rank1], NTILE(4) OVER(ORDER BY score DESC) AS [rank2] FROM score_rank ORDER BY score DESC;