gpt4 book ai didi

sql - 查询简化 Oracle Northwind

转载 作者:搜寻专家 更新时间:2023-10-30 20:04:08 24 4
gpt4 key购买 nike

任务:

找出最多客户所属的国家/地区。

查询

SELECT country,
count(*)
FROM customers
GROUP BY country
HAVING count(*) =
(SELECT max(max_sal)
FROM
(SELECT count(*) max_sal
FROM customers
GROUP BY country)) ;

结果:

enter image description here

结果是正确的,但我认为编写查询的方式很困难

问题:有什么简单的方法可以重写这个查询。

最佳答案

我可能遗漏了一些东西,但它可以像这样简单:

SELECT *
FROM ( SELECT country, COUNT (*) max_sal
FROM customers
GROUP BY country
ORDER BY COUNT (*) DESC)
WHERE ROWNUM <= 1;

关于sql - 查询简化 Oracle Northwind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36459652/

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