如何在 sql (postgresql) 中获取每组列的最高计数

how to get top count of a column per group in sql (postgresql)

假设我有这样一个 table:

company investor
apple john
apple john
apple john
apple alex
google max
google max
google oliver

我想编写一个查询来查找每家公司的顶级投资者并得到如下响应:

company investor count
apple john 3
google max 2

显然我的例子很愚蠢,但是从数据和预期结果来看,它完全代表了我的顾虑。

更新:

正如@jarlh 所提到的,如果一家公司有不止一位顶级投资者,那么我希望得到第一个按名称排序的投资者,就像如果我们还有一行 googleoliver 然后我想得到 max2 因为名称在结果中按字母顺序排序;

你可以做到这一点

with x as (
select rank() over (partition by company order by count(*) desc, investor) as rank, company,investor,count(*) as qty from test group by company,investor)
select * from x where rank = 1

如果 2 个投资者之间的情况相等,而您想同时获得两者,则 运行 查询如下

with x as (
select rank() over (partition by company order by count(*) desc) as rank, company,investor,count(*) as qty from test group by company,investor)
select * from x where rank = 1

Result here

请使用以下查询,

SELECT 
COMPANY
,INVESTOR
,COUNTS
FROM
(
SELECT 
COMPANY
,INVESTOR
,COUNT(*) COUNTS
,DENSE_RANK() OVER (PARTITION BY COMPANY ORDER BY COUNTS DESC) RN
FROM TABLE
GROUP BY COMPANY,INVESTOR

)B
WHERE RN=1

这将 return 所有投资者姓名(如果有平局)。如果您只需要一个,您可以使用 rank() 而不是 dense_rank() 并按子句

的顺序添加其他字段