-1

目标:
用表 a 和 b 中的数据加载目标表 c。

问题:
我有两个表 a 和 b 需要迁移到同一个表 c 中。

需要一次加载一张表。当加载到表 c 时,表 a 的条件需要定义该条件最多为访问 1,而表 b 的访问最多为 2。

问题是:
您如何为表 c 制定标准?

表 c 的请求结果:

table c
----------
number access gender
--------------------
1      1      2
1      2      2
2      2      2
3      1      1
4      2      1
5      1      2
5      2      2

table a
----------
number access gender
--------------------
1      1      2
2      1      2
3      1      1
4      1      1
5      1      2


table b
----------
number access gender
--------------------
1      2      2
2      2      2
3      2      1
4      2      1
5      2      2


table c
----------
number access gender
--------------------
1      1      null
1      2      null
2      2      null
3      1      null
4      2      null
5      1      null
5      2      null
4

1 回答 1

0

I think I understand what you are trying to do, try using a couple of MERGE statements like so:

MERGE c AS target
USING a AS source 
ON (target.number = source.number, target.access = source.access) 
WHEN MATCHED THEN 
UPDATE SET target.gender = source.gender
WHEN NOT MATCHED BY target THEN 
INSERT (number, access, gender) 
VALUES (source.number, source.access, source.gender)

MERGE c AS target
USING b AS source 
ON (target.number = source.number, target.access = source.access) 
WHEN MATCHED THEN 
UPDATE SET target.gender = source.gender
WHEN NOT MATCHED BY target THEN 
INSERT (number, access, gender) 
VALUES (source.number, source.access, source.gender)
于 2013-03-06T09:02:52.297 回答