合并SQL数据?

合并SQL数据?,sql,postgresql,Sql,Postgresql,这看起来很简单,但我似乎不知道怎么做。我有两个数据集: 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

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

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, T1.Total1 + T2.Total1 AS TOTAL1, ...
FROM T1 JOIN T2 ON T1.Date = T2.Date

我猜您想要一个
完全连接

SELECT  COALESCE(T1.DATE,T2.DATE) AS DATE,
        COALESCE(T1.TOTAL1,0)+COALESCE(T2.TOTAL1,0) AS TOTAL1,
        COALESCE(T1.TOTAL2,0)+COALESCE(T2.TOTAL2,0) AS TOTAL2,
        COALESCE(T1.TOTAL3,0)+COALESCE(T2.TOTAL3,0) AS TOTAL3
FROM Table1 T1
FULL JOIN Table2 T2
    ON T1.DATE = T2.DATE

使用UNION ALL创建两个集合的1个表格。 然后按日期分组并汇总所有总数

create table set1 (
 d date,
 total1 number,
 total2 number,
 total3 number
);

create table set2 (
 d date,
 total1 number,
 total2 number,
 total3 number
);

insert into set1 (d, total1, total2, total3) values (to_date('01.06.2013', 'dd.mm.yyyy'), 0,0,5);
insert into set1 (d, total1, total2, total3) values (to_date('02.06.2013', 'dd.mm.yyyy'), 0,0,12);
insert into set1 (d, total1, total2, total3) values (to_date('03.06.2013', 'dd.mm.yyyy'), 0,0,34);
insert into set1 (d, total1, total2, total3) values (to_date('04.06.2013', 'dd.mm.yyyy'), 0,0,50);

insert into set2 (d, total1, total2, total3) values (to_date('01.06.2013', 'dd.mm.yyyy'), 1,2,0);
insert into set2 (d, total1, total2, total3) values (to_date('02.06.2013', 'dd.mm.yyyy'), 4,12,0);
insert into set2 (d, total1, total2, total3) values (to_date('03.06.2013', 'dd.mm.yyyy'), 5,12,0);
insert into set2 (d, total1, total2, total3) values (to_date('04.06.2013', 'dd.mm.yyyy'), 6,10,0);

commit;

select d, sum(total1) as total1, sum(total2) as total2, sum(total3) as total3 from (
  select d, total1, total2, total3 from set1
  union all
  select d, total1, total2, total3 from set2
) group by d
order by d;

您可以使用类似的方法:

INSERT INTO set3 (date,Total1,Total2,Total3)
    SELECT s1.date
       ,case when s1.date=s2.date then s1.Total1+s2.Total1 end as Total1
       ,case when s1.date=s2.date then s1.Total2+s2.Total2 end as Total2
       ,case when s1.date=s2.date then s1.Total3+s2.Total3 end as Total3
    FROM set1 s1, set2 s2
    WHERE s1.date=s2.date

仅当您要添加具有相同日期的列时,此选项才起作用。它不会处理日期不同的行…

不清楚为什么不能在
date
加入。还不清楚为什么列被命名为
total
。我看不到您尝试过的SQL(即使它不工作),也看不到您正在使用的Postgres版本。。。请编辑您的问题以使其清楚。
INSERT INTO set3 (date,Total1,Total2,Total3)
    SELECT s1.date
       ,case when s1.date=s2.date then s1.Total1+s2.Total1 end as Total1
       ,case when s1.date=s2.date then s1.Total2+s2.Total2 end as Total2
       ,case when s1.date=s2.date then s1.Total3+s2.Total3 end as Total3
    FROM set1 s1, set2 s2
    WHERE s1.date=s2.date