简体   繁体   English

查找前 3 个值,但也包括 SQL 中的重复项

[英]Find top 3 values but also include duplicates in SQL

If I have a table like,如果我有一张桌子,

Names姓名 Marks分数
Liam利亚姆 50 50
Jade 55 55
John约翰 55 55
Pern佩恩 60 60
Lopa洛帕 20 20

I want to find the top 3 marks but if there are ties I want to include them and the number of people can be more than 3. So this table will become,我想找到前 3 个标记,但如果有平局,我想包括它们并且人数可以超过 3。所以这张表将变成,

Names姓名 Marks分数
Pern佩恩 60 60
Jade 55 55
John约翰 55 55
Liam利亚姆 50 50

Any suggestions how to take care of the ties part?任何建议如何照顾领带部分?

You can do something like this.你可以做这样的事情。 Let 'tblMarks' be your table name.让 'tblMarks' 成为您的表名。 Replace @N with your desired value, eg.用您想要的值替换@N,例如。 3 3

select * from tblMarks where 
Marks between
(SELECT Marks from tblMarks e1 where 
        @N-1 = (SELECT COUNT(DISTINCT Marks)from tblMarks e2 where e2.Marks > e1.Marks))
        
and
(SELECT Marks from tblMarks e1 where 
        1-1 = (SELECT COUNT(DISTINCT Marks)from tblMarks e2 where e2.Marks > e1.Marks))
        
order by Marks desc
;

Explanation:解释:

SELECT * from tblMarks e1 where 
        @N-1 = (SELECT COUNT(DISTINCT Marks)from tblMarks e2 where e2.Marks > e1.Marks) ;

Replace @N to 1 to get the first distinct highest value, @N to 2 to get the second distinct highest value and so on.将@N 替换为 1 以获得第一个不同的最高值,将 @N 替换为 2 以获得第二个不同的最高值,依此类推。

Now simply use Subquery with a between Operator between现在只需使用带有 between 运算符的子查询

  1. The Highest Marks and最高分和
  2. your desired marks你想要的分数

So if you give 3 in the first query it will be something like this因此,如果您在第一个查询中给出 3,它将是这样的

select * from tblMarks where 
Marks between 50 and 60
order by Marks Desc

As 60 is the highest and 50 is the 3rd highest marks.因为 60 是最高分,50 是第三高分。

If you are using MySQL 8 or later, then the DENSE_RANK analytic function can be used here:如果您使用的是 MySQL 8 或更高版本,则可以在此处使用DENSE_RANK分析函数:

WITH cte AS (
    SELECT *, DENSE_RANK() OVER (ORDER BY Marks DESC) rnk
    FROM yourTable
)

SELECT Names, Marks
FROM cte
WHERE rnk <= 3;

Demo 演示

To do this without using a CTE, just inline it:要在不使用 CTE 的情况下执行此操作,只需将其内联:

SELECT Names, Marks
FROM
(
    SELECT *, DENSE_RANK() OVER (ORDER BY Marks DESC) rnk
    FROM yourTable
) t
WHERE rnk <= 3;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM