0

帮助!!

我编写了一个查询以将视图中的数据合并到数据仓库表中

该视图包含来自 3 个不同数据库的数据

如果 TFN(税号,不是 pk)相同,我需要更新数据仓库中的一行

然而,只有 2 个表(在澳大利亚)存储 TFN,第三个(不在澳大利亚)没有,并且这些行的视图中的 TFN 为空

我已经尝试了一些东西,但无法得到我想要的结果

此尝试运行正常,但插入具有相同 TFN 的第二行

MERGE INTO DWCUSTOMER d
using CUST_ALL_VIEW V
ON ((d.TFN = v.TFN AND d.COUNTRY = 'Australia') OR (d.CUSTOMER_ID = v.CUSTOMER_ID AND d.COUNTRY <> 'Australia'))
when matched then update set D.OCCUPATION = V.OCCUPATION, D.CITY = V.CITY, D.STATE = V.STATE,
d.POSTAL_CODE = v.POSTAL_CODE, d.REGION = v.REGION, d.COUNTRY = v.COUNTRY
when not matched then insert
(CUSTOMER_KEY, CUSTOMER_ID, CITY, STATE, POSTAL_CODE, GENDER, TFN, OCCUPATION, REGION, COUNTRY)
values
(DWCUSTSEQ.NEXTVAL, v.CUSTOMER_ID, v.CITY, v.STATE, v.POSTAL_CODE, v.GENDER, v.TFN, v.OCCUPATION, v.REGION, v.COUNTRY);

我所做的另一次尝试不起作用(正如我后来发现的那样,你不能在许多列中的一个上拥有唯一/不同),但它更好地展示了我想要实现的目标(我认为:/)

merge into DWCUSTOMER D
using (select SOURCE_ROWID, TABLE_NAME, CUSTOMER_ID, CITY, STATE,
POSTAL_CODE, GENDER, REGION, COUNTRY, UNIQUE(TFN) AS TFN, OCCUPATION
from CUST_ALL_VIEW
where COUNTRY = 'Australia'
UNION
select *
from CUST_ALL_VIEW
WHERE COUNTRY <> 'Australia') v
on ((D.TFN = V.TFN and D.COUNTRY = 'Australia') or (D.CUSTOMER_ID = V.CUSTOMER_ID and D.COUNTRY <> 'Australia'))
when matched then update set D.OCCUPATION = V.OCCUPATION, D.CITY = V.CITY, D.STATE = V.STATE,
d.POSTAL_CODE = v.POSTAL_CODE, d.REGION = v.REGION, d.COUNTRY = v.COUNTRY
when not matched then insert
(CUSTOMER_KEY, CUSTOMER_ID, CITY, STATE, POSTAL_CODE, GENDER, TFN, OCCUPATION, REGION, COUNTRY)
values
(DWCUSTSEQ.NEXTVAL, v.CUSTOMER_ID, v.CITY, v.STATE, v.POSTAL_CODE, v.GENDER, v.TFN, v.OCCUPATION, v.REGION, v.COUNTRY);

建议修复??

谢谢

马特

4

1 回答 1

0

(迟到的答案是迟到的)尝试将 TFN 与此进行比较:

ISNULL(d.TFN,-9) = isnull(v.TFN,-9)

(其中 -9 显然是一个无效的 TFN - 只是为了绕过 SQL Null 比较的奥秘)

于 2013-12-12T05:04:13.443 回答