gpt4 book ai didi

postgresql - postgres如何正确使用max函数

转载 作者:行者123 更新时间:2023-11-29 14:08:04 28 4
gpt4 key购买 nike

请帮助正确使用 max,我有以下内容:

select busqueda.valorBusqueda, count(*) from busqueda where usu_id = 24 group by busqueda.valorBusqueda;

它有效,但我只想要它的最大数量,到目前为止我试过了:

select max (busqueda.valorBusqueda, count(*) from busqueda where usu_id = 24 group by busqueda.valorBusqueda);

但没有成功..

最佳答案

这里最简单的解决方案可能是使用 LIMIT 查询:

select valorBusqueda, count(*) as cnt
from busqueda
where usu_id = 24
group by valorBusqueda
order by count(*) desc
limit 1;

Postgres 不支持与 LIMIT 的关系,但如果您确实想要所有关系的最高计数,我们可以在此处使用 RANK 分析函数:

with cte as (
select valorBusqueda, count(*) as cnt, rank() over (order by count(*) desc) rnk
from busqueda
where usu_id = 24
group by valorBusqueda
)

select valorBusqueda, cnt
from cnt
where rnk = 1;

关于postgresql - postgres如何正确使用max函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58829921/

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