-2

我想合并这两个表。

如果它们与行中的值匹配,我想插入新行并更新列计数中的值

表 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
4

3 回答 3

1

如果您不介意从 Table1 中删除数据然后插入新数据,您可以这样做:

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

于 2013-09-21T12:36:08.467 回答
0

我知道它不是一个优化的解决方案,但它会解决你的问题

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
于 2013-09-21T12:00:59.527 回答
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

我希望这可以帮助你

于 2013-09-21T12:19:30.363 回答