gpt4 book ai didi

sql - 单独使用子查询会产生不同的结果

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

我必须跨两个不同的表 countrycity 编写查询。目标是获取每个地区的每个地区和该地区的人口。由于区只是每个城市的一个属性,所以我要归入属于一个区的每个城市的所有人口。

到目前为止,我的查询如下所示:

SELECT country.name, country.population, array_agg(
(SELECT (c.district, sum(city.population))
FROM city GROUP BY c.district))
AS districts
FROM country
FULL OUTER JOIN city c ON country.code = c.countrycode
GROUP BY country.name, country.population;

结果:

                    name                     | population |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              districts                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
---------------------------------------------+------------+------------------------------------------------------------------------------------------------------------------
Afghanistan | 22720000 | {"(Balkh,1429559884)","(Qandahar,1429559884)","(Herat,1429559884)","(Kabol,1429559884)"}
Albania | 3401200 | {"(Tirana,1429559884)"}
Algeria | 31471000 | {"(Blida,1429559884)","(Béjaïa,1429559884)","(Annaba,1429559884)","(Batna,1429559884)","(Mostaganem,1429559884)"
American Samoa | 68000 | {"(Tutuila,1429559884)","(Tutuila,1429559884)"}

很显然,它是世界上所有城市人口的总和。我需要以某种方式将其限制在每个地区。

但是如果我单独运行子查询

SELECT (city.district, sum(city.population)) FROM city GROUP BY city.district;

它为我提供了地区及其人口:

               row                
----------------------------------
(Bali,435000)
(,4207443)
(Dnjestria,194300)
(Mérida,224887)
(Kochi,324710)
(Qazvin,291117)
(Izmir,2130359)
(Meta,273140)
(Saint-Denis,131480)
(Manitoba,618477)
(Changhwa,354117)

我意识到它必须与我加入时使用的缩写做一些事情。我使用它是为了方便,但它似乎会产生真正的后果,因为如果我不使用它,它会给我错误

more than one row returned by a subquery used as an expression

此外,如果我使用

sum(c.population)

在子查询中它不会执行因为

aggregate function calls cannot be nested

加入时这个缩写显然变化很大

我希望有人能对此有所启发。

最佳答案

我自己解决了。

窗口函数是完成这类任务最方便的方法:

SELECT DISTINCT
country.name
, country.population
, city.district
, sum(city.population) OVER (PARTITION BY city.district)
AS district_population
, sum(city.population) OVER (PARTITION BY city.district)/ CAST(country.population as float)
AS district_share

FROM
country JOIN city ON country.code = city.countrycode
;

但它也适用于子选择:

SELECT DISTINCT
country.name
, country.population
, city.district
,(
SELECT
sum(ci.population)
FROM
city ci
WHERE ci.district = city.district
) AS district_population
,(
SELECT
sum(ci2.population)/ CAST(country.population as float)
FROM
city ci2
WHERE ci2.district = city.district
) AS district_share

FROM
country JOIN city ON country.code = city.countrycode

ORDER BY
country.name
, country.population
;

关于sql - 单独使用子查询会产生不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53031139/

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