一、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. go lang学习笔记——channel机理及调度理解

    <Go语言编程>一书介绍了libtask库,可以认为这个库等同于go的底层goroutine实现. libtask库的channel的数据结构如下: struct Alt { Channe ...

  2. 一个包的libevent流程

    //一个发包的流程 第一个包就是客户端的心跳包,现在加了版本的包 再来看看这个发包打包过程,过程坚持,但理解费劲 void NGP::OnliveTimer()//客户端心跳,5s发一次 { Send ...

  3. 各大公司广泛使用的在线学习算法FTRL详解

    各大公司广泛使用的在线学习算法FTRL详解 现在做在线学习和CTR常常会用到逻辑回归( Logistic Regression),而传统的批量(batch)算法无法有效地处理超大规模的数据集和在线数据 ...

  4. 使用jQuery动态加载js脚本

    动态加载Javascript是一项非常强大且有用的技术.这方面的主题在本站已经讨论了不少,我也经常会在一些个人项目上使用RequireJS和Dojo加载js.它们很强大,但有时候也会得不偿失.如果你使 ...

  5. samsung-smart app 开发

    http://www.samsungdforum.com/ http://seller.samsungapps.com/login/signIn.as?returnURL=%2fmain%2fsell ...

  6. Http Url Get请求方式需要对中文参数进行编码

    public static void main(String[] args) { try { String mytext = java.net.URLEncoder.encode("上海南站 ...

  7. ID3决策树---Java

    1)熵与信息增益: 2)以下是实现代码: //import java.awt.color.ICC_ColorSpace; import java.io.*; import java.util.Arra ...

  8. Using the viewport meta tag to control layout on mobile browsers

    A typical mobile-optimized site contains something like the following: <meta name="viewport& ...

  9. 全自动化的 Android 编译管线

    [编者按]Nicolas Frankel 是 hybris 的高级顾问, 在Java / J2EE 领域拥有超过10年的管理经验,本文阐述了他在使用自动化工序去构建 Android 应用程序遇到的一些 ...

  10. POJ 3320

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6496   Accept ...