Sql Server、Access資料排名的實現方法(例如:成績排名)

來源:互聯網
上載者:User

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

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

排名後的結果如下:


Access
複製代碼 代碼如下:
select name, score, (select iif(isnull(sum(1)), 1, sum(1) + 1) from score_rank where score > a.score) 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 > a.score) 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;

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.