美文网首页
178. Rank Scores

178. Rank Scores

作者: wenmingxing | 来源:发表于2018-07-23 10:21 被阅读38次
    题目链接:

    178. Rank Scores

    解析:

    刚开始看到这道题我的写法是这样的:

    SELECT Score,
        (SELECT COUNT(*) FROM Scores S1 WHERE S1.Id != S2.Id AND S1.Score >= S2.Score) AS Rank
    FROM Scores S2
    ORDER BY Rank;
    

    但是这样的输出Rank结果为:1,1,2,4,4,5。不满足题目要求,所以这里我们应该使用DISTINCT关键字来进行处理。

    题解:
    # Write your MySQL query statement below
    SELECT Score, 
        (SELECT COUNT(DISTINCT Score) FROM Scores S1 WHERE S1.Score >= S2.Score) AS Rank 
    FROM Scores S2
    ORDER BY Rank;  
    

    相关文章

      网友评论

          本文标题:178. Rank Scores

          本文链接:https://www.haomeiwen.com/subject/bvudmftx.html