0

这看起来很简单,但我似乎无法弄清楚如何去做。我有两个数据集:

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

加入表格不起作用。如果日期匹配,我需要以一种可以添加总数的方式加入他们。知道怎么做吗?

4

5 回答 5

5
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
于 2013-07-01T13:45:09.553 回答
2

我猜你想要一个FULL JOIN

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
于 2013-07-01T13:45:41.203 回答
0

您仍然想进行连接,但明确命名列,就像这样。

SELECT Date, T1.Total1 + T2.Total1 AS TOTAL1, ...
FROM T1 JOIN T2 ON T1.Date = T2.Date
于 2013-07-01T13:42:41.417 回答
0

您可以使用类似的东西:

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

它仅在您要添加具有相同日期的列时才有效。它不会处理日期不同的行...

于 2013-07-01T14:00:01.893 回答
0

使用 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;
于 2013-07-01T13:51:44.590 回答