2013-07-01 89 views
0

這似乎很簡單,但我似乎無法弄清楚如何去做。我有兩個數據集:合併SQL數據?

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 

連接表不起作用。如果日期匹配,我需要以一種方式加入總數。任何想法如何做到這一點?

+0

目前還不清楚爲什麼你不能只加入日期。目前還不清楚爲什麼你的專欄被命名爲「全部」。我看不到你已經嘗試過的SQL(即使它不起作用),也不能看到你正在使用的Postgres版本......請編輯你的問題以說清楚。 –

回答

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 
0

你仍然想要做一個連接,但明確命名列,像這樣。

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

使用UNION ALL創建兩組的1臺。 然後GROUP BY日期和總結所有總數。

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; 
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 

,當你想用同一日期列添加它才起作用。它不會處理日期不同的行...