SQL Server, Access data ranking implementation methods (for example: Score rankings) _ Database other

Source: Internet
Author: User
However, before SQL Server 2005, SQL Server 2000 did not provide this direct function for us to use, as did ACCESS.

Below we divide 2 kinds of circumstances, to write the data ranking realization process. The test data are as follows:

The results of the rankings are as follows:


Access

Copy Code code as follows:

Select name, Score, (select IIF (SUM (1), 1, sum (1) + 1) from Score_rank where score > A.score) as rank from Scor E_rank a ORDER BY score Desc

Sql server
Copy Code code as follows:

Select name, score, (select ISNULL (SUM (1), 0) + 1 from Score_rank where score > A.score) as rank to score_rank a order By score Desc

For SQL SERVER 2005 and later
Copy Code code as follows:

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 the Score_rank order by score DESC;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.