gpt4 book ai didi

sql - 合并 SQL 数据?

转载 作者:行者123 更新时间:2023-11-29 14:04:22 24 4
gpt4 key购买 nike

这看起来很简单,但我似乎不知道该怎么做。我有两个数据集:

SET1
DATE | TOTAL1 | TOTAL2 | TOTAL3
1 Jun 2013 | 0 | 0 | 5
2 Jun 2013 | 0 | 0 | 12
3 Jun 2013 | 0 | 0 | 34
4 Jun 2013 | 0 | 0 | 50

SET2
DATE | TOTAL1 | TOTAL2 | TOTAL3
1 Jun 2013 | 1 | 2 | 0
2 Jun 2013 | 4 | 12 | 0
3 Jun 2013 | 5 | 12 | 0
4 Jun 2013 | 6 | 10 | 0

我想创建第三个数据集,将这两个集合合并成以下内容:

SET3
DATE | TOTAL1 | TOTAL2 | TOTAL3
1 Jun 2013 | 1 | 2 | 5
2 Jun 2013 | 4 | 12 | 12
3 Jun 2013 | 5 | 12 | 34
4 Jun 2013 | 6 | 10 | 50

加入表是行不通的。如果日期匹配,我需要以一种将合计相加的方式加入他们。知道如何做到这一点吗?

最佳答案

SELECT
DATE,
SUM(TOTAL1) AS TOTAL1,
SUM(TOTAL2) AS TOTAL2,
SUM(TOTAL3) AS TOTAL3
FROM
(
SELECT
DATE,
TOTAL1,
TOTAL2,
TOTAL3
FROM
SET1

UNION ALL

SELECT
DATE,
TOTAL1,
TOTAL2,
TOTAL3
FROM
SET2
) SubQueryAlias
GROUP BY
DATE

关于sql - 合并 SQL 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17405890/

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