我刚接触SQL。感谢您的耐心等候。
我的查询如下:
SELECT
population. "District.Name",
sum(population. "Number") AS "Total Pop",
sum(unemployment. "Number") AS "Total Unemployment"
FROM
population
FULL JOIN unemployment ON population. "District.Code" = unemployment. "District Code"
WHERE
population. "Year" = 2017
GROUP BY
population. "District.Name",
population. "Number",
unemployment. "Number"
ORDER BY
sum(unemployment. "Number") ASC
;
但是,生成的输出如所附的屏幕快照所示:
。
我想获取每个地区的总流行/总失业人数,而不是当前的情况。 我还试图找出是什么引起了地区名称的分裂。我尝试了各种组合,但尚未获得理想的结果。 有什么建议吗?预先感谢。
答案 0 :(得分:0)
您可以在下面尝试-您无需在分组依据中添加population. "Number", unemployment. "Number"
列,因为您正在这些列上使用汇总
SELECT
population. "District.Name",
sum(population. "Number") AS "Total Pop",
sum(unemployment. "Number") AS "Total Unemployment"
FROM
population
FULL JOIN unemployment ON population. "District.Code" = unemployment. "District Code"
WHERE
population. "Year" = 2017
GROUP BY
population. "District.Name",
ORDER BY
sum(unemployment. "Number")
答案 1 :(得分:0)
您只需要按District.Name分组。由于您已按“人口和失业”进行分组,因此它们还将显示唯一的行。
希望这行得通!
SELECT
population. "District.Name",
sum(population. "Number") AS "Total Pop",
sum(unemployment. "Number") AS "Total Unemployment"
FROM
population FULL JOIN unemployment ON population. "District.Code" = unemployment. "District Code"
WHERE
population. "Year" = 2017
GROUP BY
population. "District.Name",
ORDER BY
sum(unemployment. "Number") ASC
答案 2 :(得分:0)
FULL JOIN
几乎不再需要,当然对于带有WHERE
子句的查询也不需要。我怀疑INNER JOIN
足够。
但是您将获得多行。这表明在JOIN
之后的 处进行汇总将使某些事情变得多余。汇总之前做JOIN
:
SELECT p."District.Name", p."Total Pop",
u."Total Unemployment",
(p."Total Pop" * 1.0 / u."Total Unemployment") as ratio
FROM (SELECT p."District.Name", SUM(p."Number") AS "Total Pop"
FROM population p
WHERE p."Year" = 2017
GROUP BY p."District.Name"
) p INNER JOIN
(SELECT u."District.Name", SUM(u."Number") AS "Total Unemployment"
FROM unemployment u
GROUP BY u."District.Name"
) u
ON p."District.Code" = u."District Code"
ORDER BY u."Total Unemployment" ASC;
我怀疑您的人数仍然不会达到您的期望,因为您只针对人口筛选年份。我怀疑两个数字都应具有相似的过滤器。如果是这种情况,请询问另一个问题。提供样本数据和所需结果。
在样式方面: