SQL Server implementations LeetCode 178 ranking scores

178. scores ranking

SQL schema
to write a SQL query to achieve the score rankings. If the two scores are identical, the two scores rank (Rank) the same. Note that in the bisecting a ranking should be the next consecutive integer values. In other words, there should be no "gap" between the ranking.

+----+-------+
| Id | Score |
+----+-------+
| 1  | 3.50  |
| 2  | 3.65  |
| 3  | 4.00  |
| 4  | 3.85  |
| 5  | 4.00  |
| 6  | 3.65  |
+----+-------+

For example, according to the table given Scores, your query should return (by score from highest to lowest):

+-------+------+
| Score | Rank |
+-------+------+
| 4.00  | 1    |
| 4.00  | 1    |
| 3.85  | 2    |
| 3.65  | 3    |
| 3.65  | 3    |
| 3.50  | 4    |
+-------+------+

PS:
b is not repeated query results, and descending sequence of numbers

SELECT b.Score AS Score,b.Rank FROM Scores INNER JOIN
 (
SELECT Score,ROW_NUMBER() OVER (ORDER BY Score DESC) AS Rank FROM
 (SELECT DISTINCT(Score) AS Score FROM Scores)
a
)b ON b.Score = Scores.Score
ORDER BY b.Rank

 


Released 1297 original articles · won praise 10000 + · views 960 000 +

Guess you like

Origin blog.csdn.net/a1439775520/article/details/104466319