sql server 2008 recognize DENSE_RANK

There is a demand score ranking, the same score ranking considered the same rank, while ranking ranking in no interval.

Create a table

CREATE TABLE result
(
id INT PRIMARY KEY IDENTITY,
score VARCHAR(20)
)

Insert the following data:

The first idea is to think of

1. packet ordering

2. After the packet sorting table with the original table to sort the left link

code show as below:

WITH temp AS(
SELECT ROW_NUMBER()OVER(ORDER BY score DESC)rank,score FROM result GROUP BY score)
SELECT rank,result.score FROM temp LEFT  JOIN result on result.score=temp.score ORDER BY score DESC

The result is

After found to be more convenient way, i.e. with DENSE_RANK, this function can be achieved by sorting purposes, as follows:

select Score, dense_rank()OVER (order by score desc) as Rank from result

The results with the above, there is access to the back of a rank function, which is sort of, but check out the ranking of results is ranked at intervals, as follows:

SELECT score,RANK() OVER(ORDER BY score DESC) AS RANK FROM result

The results are as follows ranking ranking incoherent:

 

Guess you like

Origin www.cnblogs.com/97310ZT/p/10974593.html