【问题标题】:How to avoid using nested aggregate functions?如何避免使用嵌套聚合函数?
【发布时间】:2018-05-31 10:53:13
【问题描述】:

我需要一些帮助,我相信你们知道怎么做: 让我们从表结构开始:

author(name, nationality, Gender);
article(title, year, conference);
publication(articleTitle, authorName);

我需要知道发表文章最多的作者的性别。顺便说一句,我使用的是 PostgreSQL,不知道这是否重要。

这是我的想法:

select gender from author
join publication on(name = authorName)
having (count(articleTitle) = max(count(articleTitle)))
group by gender

现在,我知道我不能使用嵌套聚合函数,这就是我尝试使用嵌套选择的原因,例如 select gender where gender in (another select) 但我没有设法避免聚合函数问题。 希望您能帮帮我,谢谢

【问题讨论】:

  • 样本数据和期望的结果会有所帮助。

标签: sql postgresql aggregate-functions


【解决方案1】:

此查询为您提供作者,按出版物数量排序:

select a.name, a.gender, count(*) as num_publications
from author a join
     publication p
     on a.name = p.authorName
group by a.name, a.gender
order by num_publications desc;

如果你想要前三名,请使用fetch firstlimit

select a.name, a.gender, count(*) as num_publications
from author a join
     publication p
     on a.name = p.authorName
group by a.name, a.gender
order by num_publications desc
fetch first 3 rows only;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 2020-01-14
    • 2014-01-26
    相关资源
    最近更新 更多