SQL 中的 SUM 和 GROUP BY 合计问题
SUM and GROUP BY in SQL totaling issue
我正在处理两个不同的 covid 数据集,其中包括来自所有国家/地区的报告数据。我遇到的问题是 sum
和 group by
函数,其中 SQL 对所有分组的行求和,这不必要地增加了输出。对于下面的示例,总死亡人数应为 4,849,总病例数为 17,441。
*注意:数据集包括每个国家的 city/province,下面已经按国家分组
CREATE TABLE covid.deaths(
country varchar(255),
deaths int
);
CREATE TABLE covid.confirmed_cases(
country varchar(255),
cases int
);
*country* *deaths* *cases*
China 0 747
China 0 519
China 0 1500
China 0 264
China 1 159
China 3 1992
China 2 1008
China 4 1632
China 7 1650
China 6 190
China 213 1260
China 8 1197
China 58 3260
China 13 362
China 22 703
China 4512 998
然而,当我 运行 下面的查询时,我得到 279,056 个病例总数和 77,584 个死亡病例。在尝试自行解决此问题时,我删除了 sum
函数之一(但保留了日期)并发现输出 SQL 正在为所有行填充列中的总数,然后当两个 sum
函数都存在时对这些列求和(基本上 SQL 使用的方程是 total deaths or cases x number of rows
)。
SELECT
COALESCE(d.country_region, "Unknown") AS country,
SUM(d._11_16_21) as deaths,
SUM(c._11_16_21) as cases
FROM `covid.deaths` as d
JOIN `covid.confirmed_cases` as c
ON d.country_region = c.country_region
WHERE d.country_region = "China"
GROUP BY
d.country_region
删除案例 sum
函数的输出
SELECT
COALESCE(d.country_region, "Unknown") AS country,
SUM(d._11_16_21) as deaths,
c._11_16_21 as cases
FROM `covid.deaths` as d
JOIN `covid.confirmed_cases` as c
ON d.country_region = c.country_region
WHERE d.country_region = "China"
GROUP BY
d.country_region, c._11_16_21
*country* *deaths* *cases*
China 4849 747
China 4849 519
China 4849 1500
China 4849 264
China 4849 159
China 4849 1992
China 4849 1008
China 4849 1632
China 4849 1650
China 4849 190
China 4849 1260
China 4849 1197
China 4849 3260
China 4849 362
China 4849 703
China 4849 998
有没有办法让 SQL 只显示唯一的总和作为输出?
*注意:正在使用 Coalesce,因为有一些国家名称是 null
在将 covid 病例与死亡合并之前将它们分别求和
SELECT
COALESCE(d.country_region, c.country_region, 'Unknown') AS country,
d.deaths,
c.cases
FROM (
SELECT country_region
, SUM(`_11_16_21`) as deaths
FROM `covid.deaths`
WHERE country_region = 'China'
GROUP BY country_region
) as d
JOIN (
SELECT country_region
, SUM(`_11_16_21`) as cases
FROM `covid.confirmed_cases`
WHERE country_region = 'China'
GROUP BY country_region
) as c
ON c.country_region = d.country_region
我正在处理两个不同的 covid 数据集,其中包括来自所有国家/地区的报告数据。我遇到的问题是 sum
和 group by
函数,其中 SQL 对所有分组的行求和,这不必要地增加了输出。对于下面的示例,总死亡人数应为 4,849,总病例数为 17,441。
*注意:数据集包括每个国家的 city/province,下面已经按国家分组
CREATE TABLE covid.deaths(
country varchar(255),
deaths int
);
CREATE TABLE covid.confirmed_cases(
country varchar(255),
cases int
);
*country* *deaths* *cases*
China 0 747
China 0 519
China 0 1500
China 0 264
China 1 159
China 3 1992
China 2 1008
China 4 1632
China 7 1650
China 6 190
China 213 1260
China 8 1197
China 58 3260
China 13 362
China 22 703
China 4512 998
然而,当我 运行 下面的查询时,我得到 279,056 个病例总数和 77,584 个死亡病例。在尝试自行解决此问题时,我删除了 sum
函数之一(但保留了日期)并发现输出 SQL 正在为所有行填充列中的总数,然后当两个 sum
函数都存在时对这些列求和(基本上 SQL 使用的方程是 total deaths or cases x number of rows
)。
SELECT
COALESCE(d.country_region, "Unknown") AS country,
SUM(d._11_16_21) as deaths,
SUM(c._11_16_21) as cases
FROM `covid.deaths` as d
JOIN `covid.confirmed_cases` as c
ON d.country_region = c.country_region
WHERE d.country_region = "China"
GROUP BY
d.country_region
删除案例 sum
函数的输出
SELECT
COALESCE(d.country_region, "Unknown") AS country,
SUM(d._11_16_21) as deaths,
c._11_16_21 as cases
FROM `covid.deaths` as d
JOIN `covid.confirmed_cases` as c
ON d.country_region = c.country_region
WHERE d.country_region = "China"
GROUP BY
d.country_region, c._11_16_21
*country* *deaths* *cases*
China 4849 747
China 4849 519
China 4849 1500
China 4849 264
China 4849 159
China 4849 1992
China 4849 1008
China 4849 1632
China 4849 1650
China 4849 190
China 4849 1260
China 4849 1197
China 4849 3260
China 4849 362
China 4849 703
China 4849 998
有没有办法让 SQL 只显示唯一的总和作为输出?
*注意:正在使用 Coalesce,因为有一些国家名称是 null
在将 covid 病例与死亡合并之前将它们分别求和
SELECT
COALESCE(d.country_region, c.country_region, 'Unknown') AS country,
d.deaths,
c.cases
FROM (
SELECT country_region
, SUM(`_11_16_21`) as deaths
FROM `covid.deaths`
WHERE country_region = 'China'
GROUP BY country_region
) as d
JOIN (
SELECT country_region
, SUM(`_11_16_21`) as cases
FROM `covid.confirmed_cases`
WHERE country_region = 'China'
GROUP BY country_region
) as c
ON c.country_region = d.country_region