一、Rank Scores

Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks.

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

For example, given the above Scores table, your query should generate the following report (order by highest score):+-------+------+

| Score | Rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+
分析:题意为 编写SQL对分数进行排序。如果两个分数相等,其排名应相同。注意在排名相等的分数之后,下一个排名的数值应该连续。换言之,排名之间不应该有“洞”(跳跃)。
思路:使用mysql的自定义变量。
# Write your MySQL query statement below
select Score,Rank from
(
SELECT Score,
CASE
WHEN @dummy <=> Score THEN @Rank := @Rank
ELSE @Rank := @Rank +1
END AS Rank,@dummy := Score as dummy
FROM
(SELECT @Rank := 0,@dummy := NULL) r,
Scores
ORDER BY Score DESC
) AS C

或者:

# Write your MySQL query statement below
SELECT Scores.Score, COUNT(Ranking.Score) AS RANK
FROM Scores
, (
SELECT DISTINCT Score
FROM Scores
) Ranking
WHERE Scores.Score <= Ranking.Score
GROUP BY Scores.Id, Scores.Score
ORDER BY Scores.Score DESC;

  

  

 

leetcode Database3的更多相关文章

  1. leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)

    一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...

  2. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  3. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  4. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  5. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  6. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  7. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  8. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  9. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

随机推荐

  1. android控件---自定义带文本的ImageButton

    由于SDK提供的ImageButton只能添加图片,不能添加文字:而Button控件添加的文字只能显示在图片内部:当我们需要添加文字在图片外部时就不能满足我们的需求了,顾只能自己写个自定义ImageB ...

  2. flex Chrome flash调试时 出现Shockwave flash has crashed的解决办法

    在Chrome中输入:chrome://plugins/     PPAPI的Flash Player停用. 使用NPAPI的Flash player. 这里好像没有显示是Debug版本. 但是我在调 ...

  3. python开发中常用的框架

    以下是15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python Web应用开发框架 Django 应该是最出名的 ...

  4. HTTP报头详解

    HTTP头字段包括4类:      general-header ; 通用报头      request-header ; 请求报头      response-header ; 响应报头      ...

  5. mysql5日期类型datetime查询范围值

    1.DATE_FORMAT函数 SELECT a.create_time FROM account_log a WHERE a.create_time >= DATE_FORMAT('2014- ...

  6. 13test07;字符排序,去重,三三输出

    #include<iostream> #include<string> using namespace std; void buddle(char*,int);//对输入字符的 ...

  7. XHProf的安装和使用(PHP性能测试神器)

    XHProf是Facebook开发的性能调试工具,帮助我们的PHP程序性能调优,更加健壮.XHProf安装和使用方法将在本章讲解.XHProf是PHP的PECL扩展.没有XDeBug那些耗费资源,更加 ...

  8. Selenium中expected_conditions下text_to_be_present_in_element_value方法的使用

    text_to_be_present_in_element: 判断某个元素中的text是否包含了预期的字符串 text_to_be_present_in_element_value: 判断某个元素中的 ...

  9. PHP命名空间(Namespace)

    http://www.jb51.net/article/36389.htm 字符串形式的动态调用方式 //魔法常量__NAMESPACE__的值是当前空间名称 //可以组合成字符串并调用 $comme ...

  10. 制作类似DataGrid自定义控件

    首先看一下.net自带的DataGrid,想想如何应该怎样才能实现那样的展现形式. 1)需要以网格形式显示内容. 2)网格的宽度.高度可以定义. 3)可以显示滚动条. 4)单击可以选中某个单元格. 当 ...