我有下表:
Keyword_name Conference_name
------------|-------------
value1 | conference1
value1 | conference1
value2 | conference1
value3 | conference1
value2 | conference2
value1 | conference3
.........................
现在,我想找到每个会议的关键字的最大出现次数。我怎么能这样做?
期望的结果应如下所示:
Conference_name | Keyword_maximum_occurrence
conference1 | value1
conference2 | value2
答案 0 :(得分:3)
这在PostgreSQL中称为mode()
有序集合聚合函数:
SELECT
Conference_name,
mode() WITHIN GROUP (ORDER BY Keyword_name) AS Keyword_maximum_occurrence
FROM my_table
GROUP BY Conference_name
Background on the mode function here
MODE()
(单个结果)与RANK()
(多个结果)的说明与基于RANK()
的查询(like in GurV's answer here)不同,我的答案将根据ORDER BY
子句仅返回 first 模式。因此,如果有多个关键字经常出现,则此答案将仅返回第一个。根据您的要求,这可能仍然没问题。
答案 1 :(得分:1)
试试这个:
select
conference_name,
keyword_name
from (
select
conference_name,
keyword_name,
rank() over (partition by conference_name order by count(*) desc) rnk
from your_table
group by
conference_name,
keyword_name
) t where rnk = 1;
它根据conference_name中相同关键字的出现次数分配排名,然后过滤以获得排名最高的行。
演示 @ RexTester