gpt4 book ai didi

SQL 获取列值等于最大值的行

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

我通过查询select count(category), name from product natural join supplier group by name;得到了下表;:

 count |   nome    
-------+-----------
1 | CandyCorp
1 | Nike
1 | DrinksInc
7 | Mutante
1 | Colt
7 | Mazzetti

现在我只想获取计数等于计数列最大值的行(在本例中为 7),得到:

 count |   nome    
-------+-----------
7 | Mutant
7 | Mazzetti

编辑:我通过创建一个临时表让它工作:

create table auxtable as (select count(categoria),name from product natural join supplier group by name);

select name from auxtable a1 join (select max(count) as countMax from auxtable) a2 on a1.count=a2.countMax;

drop table auxtable;

有没有办法在单个查询中做到这一点?

最佳答案

您可以使用 CTE 代替临时表:

with auxtable as (select count(categoria),name from product natural join supplier group by name)
select name from auxtable a1
join (select max(count) as countMax from auxtable) a2 on a1.count=a2.countMax;

关于SQL 获取列值等于最大值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47612801/

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