如何选择 COUNT DISTINCT 为 MAX 的行?

Posted

技术标签:

【中文标题】如何选择 COUNT DISTINCT 为 MAX 的行?【英文标题】:How to SELECT lines where COUNT DISTINCT is MAX? 【发布时间】:2021-03-06 07:56:40 【问题描述】:

这可能是一个简单的问题,但基本上我想在 SQL SERVER 中只选择出现次数最多的行。 我有一个产生以下数据的查询:

SELECT [JOB ROLE], [CITY], COUNT(DISTINCT([EMPLOYEE_ID])) as [COUNT]
FROM MyTable
GROUP BY [JOB ROLE], [CITY]

我想达到以下结果,仅选择 EMPLOYEE_ID 的 COUNT DISTINCT 为 MAX 的行:

非常感谢您就如何以最简单的方式到达这里的友好帮助和建议!

【问题讨论】:

DISTINCT 不是一个函数,它是一个集合量词。跳过那些额外的括号,只需写 COUNT(DISTINCT [EMPLOYEE_ID]) 以使代码更清晰。 多组共享最大应该怎么办?是否有平局规则,或者所有组都返回,或者,... 【参考方案1】:

您可以使用TOP WITH TIESRANK()

SELECT TOP (1) WITH TIES [JOB ROLE], [CITY], COUNT(DISTINCT [EMPLOYEE_ID]) as [COUNT]
FROM MyTable
GROUP BY [JOB ROLE], [CITY]
ORDER BY RANK() OVER (ORDER BY COUNT(DISTINCT [EMPLOYEE_ID]) DESC;

或者,使用子查询和RANK()

SELECT [JOB ROLE], [CITY], [COUNT]
FROM (SELECT [JOB ROLE], [CITY],
             COUNT(DISTINCT [EMPLOYEE_ID]) as [COUNT],
             RANK() OVER (ORDER BY COUNT(DISTINCT [EMPLOYEE_ID]) DESC) as seqnum
      FROM MyTable
      GROUP BY [JOB ROLE], [CITY]
     ) jc
WHERE seqnum = 1;

编辑:

这个问题似乎变成了每个城市最常见的工作角色。上面的查询很容易修改:

SELECT TOP (1) WITH TIES [JOB ROLE], [CITY], COUNT(DISTINCT [EMPLOYEE_ID]) as [COUNT]
FROM MyTable
GROUP BY [JOB ROLE], [CITY]
ORDER BY RANK() OVER (PARTITION BY [JOB ROLE] ORDER BY COUNT(DISTINCT [EMPLOYEE_ID]) DESC;

或者,使用子查询和RANK()

SELECT [JOB ROLE], [CITY], [COUNT]
FROM (SELECT [JOB ROLE], [CITY],
             COUNT(DISTINCT [EMPLOYEE_ID]) as [COUNT],
             RANK() OVER (PARTITION BY [JOB ROLE], ORDER BY COUNT(DISTINCT [EMPLOYEE_ID]) DESC) as seqnum
      FROM MyTable
      GROUP BY [JOB ROLE], [CITY]
     ) jc
WHERE seqnum = 1;

【讨论】:

感谢您的建议!不幸的是,这仅返回给我一行,选择最常见的 [JOB ROLE] 和 [CITY],而不是最常见的 [CITY] 的所有 [JOB ROLE] 值。 @gonzika 这不是你要求的吗?在查看所需的输出时,我认为这是您的问题,所以我认为这个答案是有效的 @GordonLinoff 问题与首次发布时相同:***.com/posts/64971175/revisions【参考方案2】:

如果你不关心平局,你可以使用 FIRST_VALUE() 窗口函数:

SELECT DISTINCT [JOB ROLE], 
       FIRST_VALUE([CITY]) OVER (PARTITION BY [JOB ROLE] ORDER BY COUNT(DISTINCT([EMPLOYEE_ID])) DESC)
FROM MyTable
GROUP BY [JOB ROLE], [CITY]

【讨论】:

以上是关于如何选择 COUNT DISTINCT 为 MAX 的行?的主要内容,如果未能解决你的问题,请参考以下文章

LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg (转)

MySQL

选择 Count (distinct col) 查询以显示结果中的行数和列数 - postgresql

聚合函数

如何在fuelPHP Orm模型中选择MAX或COUNT

Access/SQL - 绕过 COUNT(不同)