2013-09-21 101 views
-2

我想合併這兩個表格。在PostgreSQL中加入兩個表格

我要插入的新行,並更新在列計數的值,如果它們匹配在行中的值

表1是一個目標,表2是源。

表1

c1 c2 c3 c4 c5 number 
1 2 3 4 5  3 
2 3 4 5 6  2 
2 3 5 6 7  2 

表2

c1 c2 c3 c4 c5 number 
1 3 4 5 6  3 
1 2 3 4 5  2 

可以選擇查詢的格式返回數據順序下面

結果(表1)

c1 c2 c3 c4 c5 number 
1 2 3 4 5  5 
1 3 4 5 6  3 
2 3 4 5 6  2 
2 3 5 6 7  2 
+2

「的問題,要求代碼必須表現出對問題的理解最小正在解決。包括嘗試的解決方案,爲什麼他們不工作,以及預期的結果「 – zero323

+0

在sql中,count()是一個聚合函數的名稱,最好不要用它作爲列名 – wildplasser

回答

1

,如果你不介意刪除從表1的數據,然後插入新的數據,你可以這樣做:

with cte1 as (
    delete from Table1 
    returning * 
), cte2 as (
    select c1, c2, c3, c4, c5, cnt from cte1 
    union all 
    select c1, c2, c3, c4, c5, cnt from Table2 
) 
insert into Table1 
select c1, c2, c3, c4, c5, sum(cnt) 
from cte2 
group by c1, c2, c3, c4, c5; 

sql fiddle demo

,如果你真的想更新/插入數據時,可以這樣做:

with cte_upsert as (
    update Table1 as T1 set 
     cnt = T1.cnt + T2.cnt 
    from Table2 as T2 
    where 
     T1.c1 = T2.c1 and T1.c2 = T2.c2 and 
     T1.c3 = T2.c3 and T1.c4 = T2.c4 and 
     T1.c5 = T2.c5 
    returning T1.* 
) 
insert into Table1 
select T2.c1, T2.c2, T2.c3, T2.c4, T2.c5, T2.cnt 
from Table2 as T2 
where 
    not exists (
     select * 
     from cte_upsert as T1 
     where 
      T1.c1 = T2.c1 and T1.c2 = T2.c2 and 
      T1.c3 = T2.c3 and T1.c4 = T2.c4 and 
      T1.c5 = T2.c5 
); 

sql fiddle demo

,或者你可以做最明顯的一個:

update Table1 as T1 set 
    cnt = T1.cnt + T2.cnt 
from Table2 as T2 
where 
    T1.c1 = T2.c1 and T1.c2 = T2.c2 and 
    T1.c3 = T2.c3 and T1.c4 = T2.c4 and 
    T1.c5 = T2.c5; 

insert into Table1 
select T2.c1, T2.c2, T2.c3, T2.c4, T2.c5, T2.cnt 
from Table2 as T2 
where 
    not exists (
     select * 
     from Table1 as T1 
     where 
      T1.c1 = T2.c1 and T1.c2 = T2.c2 and 
      T1.c3 = T2.c3 and T1.c4 = T2.c4 and 
      T1.c5 = T2.c5 
); 

sql fiddle demo

0

我知道它不是一個ptimize解決方案,但它會解決你的問題

select 
    a.c1 as c1 , a.c2 as c2, a.c3 as c3, a.c4 as c4, a.c5 as c5, a.count + b.count as count 
from 
    t1 a 
     join 
    t2 b ON (a.c1 = b.c1 and a.c2 = b.c2 
     and a.c3 = b.c3 
     and a.c4 = b.c4 
     and a.c5 = b.c5) 
union all 
(select 
    a.c1 as c1 , a.c2 as c2, a.c3 as c3, a.c4 as c4, a.c5 as c5, a.count as count 
from 
    t1 a 
     LEFT join 
    t2 b ON (a.c1 = b.c1 and a.c2 = b.c2 
     and a.c3 = b.c3 
     and a.c4 = b.c4 
     and a.c5 = b.c5) 
WHERE b.c1 is null) 
UNION all 
select 
    a.c1 as c1 , a.c2 as c2, a.c3 as c3, a.c4 as c4, a.c5 as c5, a.count as count 
from 
    t2 a 
     LEFT join 
    t1 b ON (a.c1 = b.c1 and a.c2 = b.c2 
     and a.c3 = b.c3 
     and a.c4 = b.c4 
     and a.c5 = b.c5) 
WHERE b.c1 is null 
0

目前尚不清楚,如果「數」是一個以前的查詢或簡單場的聚合函數,但是,如果它是一個簡單的領域,如以下數據:

CREATE TABLE table1(
    c1 integer, 
    c2 integer, 
    c3 integer, 
    c4 integer, 
    c5 integer, 
    count integer 
); 

INSERT INTO table1 VALUES (1,2,3,4,5,3); 
INSERT INTO table1 VALUES (2,3,4,5,6,2); 
INSERT INTO table1 VALUES (2,3,5,6,7,2); 

CREATE TABLE table2(
    c1 integer, 
    c2 integer, 
    c3 integer, 
    c4 integer, 
    c5 integer, 
    count integer 
); 
INSERT INTO table2 VALUES (1,3,4,5,6,3); 
INSERT INTO table2 VALUES (1,2,3,4,5,2); 

你可以通過這種方式獲得您的數據:

SELECT c1, c1,c2, c3, c4,c5,SUM(count) AS count 
FROM (
    SELECT * FROM table1 
    UNION ALL 
    SELECT * FROM table2) AS foo 
GROUP BY c1, c2, c3, c4, c5 
ORDER BY c1, c2, c3, c4, c5 

我希望這可以幫助你