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