From b2f710e5b464ce78a5e767f54f50d5069ff7c2b4 Mon Sep 17 00:00:00 2001 From: zeek <984294471@qq.com> Date: Wed, 15 Apr 2020 16:32:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E6=95=B0=E6=8E=92=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql/scoresRange.sql | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 sql/scoresRange.sql diff --git a/sql/scoresRange.sql b/sql/scoresRange.sql new file mode 100644 index 0000000..4ddbe92 --- /dev/null +++ b/sql/scoresRange.sql @@ -0,0 +1,32 @@ +/** +编写一个 SQL 查询来实现分数排名。 +如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个 +名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。 + ++----+-------+ +| Id | Score | ++----+-------+ +| 1 | 3.50 | +| 2 | 3.65 | +| 3 | 4.00 | +| 4 | 3.85 | +| 5 | 4.00 | +| 6 | 3.65 | ++----+-------+ +例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列): ++-------+------+ +| Score | Rank | ++-------+------+ +| 4.00 | 1 | +| 4.00 | 1 | +| 3.85 | 2 | +| 3.65 | 3 | +| 3.65 | 3 | +| 3.50 | 4 | ++-------+------+ +*/ +select a.Score as Score, +(select count(distinct b.Score) from Scores b where b.Score >= a.Score) as Rank +from Scores a +order by a.Score DESC +