简体   繁体   English

合并SQL数据?

[英]Merging SQL data?

This seems so simple, but I can't seem to figure out how to do it. 这似乎很简单,但是我似乎无法弄清楚该怎么做。 I have two data sets: 我有两个数据集:

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

I want to create a third dataset the merges these two sets into the following: 我想创建第三个数据集,将这两个集合合并为以下内容:

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

Joining the tables does not work. 加入表不起作用。 I need to join them in a way that will add the totals if the dates match up. 我需要以某种方式加入他们,如果日期匹配,它们将加总。 Any idea how to do this? 任何想法如何做到这一点?

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

I'm guessing that you want a FULL JOIN : 我猜您想要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

You still want to do a join but name the columns explicitly, something like this. 您仍然想进行联接,但要显式命名列,类似这样。

SELECT Date, T1.Total1 + T2.Total1 AS TOTAL1, ...
FROM T1 JOIN T2 ON T1.Date = T2.Date

Use UNION ALL to create 1 table of both sets. 使用UNION ALL创建两个集合的1个表。 Then GROUP BY date and SUM up all totals. 然后按日期分组并总计所有总计。

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;

You can use something similar: 您可以使用类似的内容:

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

It works only when you want to add columns with same date. 仅当您要添加具有相同日期的列时,它才有效。 It will not handle rows where the date is different... 它不会处理日期不同的行...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM