gpt4 book ai didi

Postgresql 嵌套选择 max(sum())

转载 作者:行者123 更新时间:2023-11-29 13:19:46 24 4
gpt4 key购买 nike

我的查询:

select table.attribute, count(table.attribute) AS cnt from table
group by table.attribute
order by cnt desc;

输出是这样的:

 attribute | cnt 
-----------+-----
A | 2
B | 2
G | 1
F | 1

但我只想要最大值(A 和 B)。

最佳答案

您可以使用单层嵌套来做到这一点:

select attribute, 
cnt
from (
select attribute,
count(*) AS cnt,
max(count(*)) over () as max_cnt
from t
group by attribute
) t
where cnt = max_cnt;

关于Postgresql 嵌套选择 max(sum()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44087973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com