178. Rank Scores#1

Solution#1

# Write your MySQL query statement below
SELECT 
    Score, 
    (SELECT COUNT(DISTINCT Score) FROM Scores WHERE S.Score <= Score) Rank 
From Scores S 
ORDER BY Score DESC

Solution#2

# Write your MySQL query statement below
SELECT s.Score, count(distinct t.Score) Rank
From Scores s JOIN Scores t ON s.Score <= t.Score
GROUP BY s.Id
ORDER BY s.Score DESC

第一種方法更快
第二種方法中,必須加上GROUP BY,否則count後的結果中會有null和0;

發佈了120 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章