Sql 如何避免使用嵌套聚合函数?

Sql 如何避免使用嵌套聚合函数?,sql,postgresql,aggregate-functions,Sql,Postgresql,Aggregate Functions,我需要一些帮助,我相信你们知道怎么做: 让我们从表结构开始: author(name, nationality, Gender); article(title, year, conference); publication(articleTitle, authorName); 我需要知道出版数量最多的作者的性别。顺便说一下,我正在使用PostgreSQL,不知道这是否重要 以下是我的想法: select gender from author join publication on(name =

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

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,比如
selectgender,其中gender在(另一个select)
中,但我没有设法避免聚合函数的问题。
希望您能帮助我,谢谢您

此查询将按出版物数量排序获得作者:

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;
如果您想要前三名,请使用
先获取
限制

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;

样本数据和预期结果将有所帮助。