我在Teradata SQL Server中运行查询时收到此错误消息“失败7547目标行由多个源行更新。”
我知道我必须删除重复的行,但是我不知道该怎么做。
MERGE INTO ${DTTK_COMMON_default_database}.LA1DB51CTERELCTE AS TGT
USING (SELECT SWTBAJAF
,NUMCLIENTE
,CSI
,CTR_NUMCSIR
,CTE_NUMREL
,CTR_TIPOREL
,CTR_NUMNOM
,CTR_TIPPAR
,CTR_PORCENT
,CTR_PUESTO
,CTE_FEALTA_2K
,CTE_FEMODI_2K
,FECINF
FROM ${DTTK_COMMON_default_database}.LA1DB51CTERELCTE_STG)
AS SRC
ON
TGT.NUMCLIENTE = SRC.NUMCLIENTE AND
TGT.CSI = SRC.CSI AND
TGT.CTR_NUMCSIR = SRC.CTR_NUMCSIR AND
TGT.CTE_NUMREL = SRC.CTE_NUMREL AND
TGT.CTR_TIPOREL = SRC.CTR_TIPOREL
WHEN MATCHED THEN UPDATE
SET
SWTBAJAF = SRC.SWTBAJAF
,CTR_NUMNOM = SRC.CTR_NUMNOM
,CTR_TIPPAR = SRC.CTR_TIPPAR
,CTR_PORCENT = SRC.CTR_PORCENT
,CTR_PUESTO = SRC.CTR_PUESTO
,CTE_FEALTA_2K = SRC.CTE_FEALTA_2K
,CTE_FEMODI_2K = SRC.CTE_FEMODI_2K
,FECINF = SRC.FECINF
WHEN NOT MATCHED THEN INSERT
(SWTBAJAF
,NUMCLIENTE
,CSI
,CTR_NUMCSIR
,CTE_NUMREL
,CTR_TIPOREL
,CTR_NUMNOM
,CTR_TIPPAR
,CTR_PORCENT
,CTR_PUESTO
,CTE_FEALTA_2K
,CTE_FEMODI_2K
,FECINF)
VALUES
(SRC.SWTBAJAF
,SRC.NUMCLIENTE
,SRC.CSI
,SRC.CTR_NUMCSIR
,SRC.CTE_NUMREL
,SRC.CTR_TIPOREL
,SRC.CTR_NUMNOM
,SRC.CTR_TIPPAR
,SRC.CTR_PORCENT
,SRC.CTR_PUESTO
,SRC.CTE_FEALTA_2K
,SRC.CTE_FEMODI_2K
,SRC.FECINF);
结果,我需要从表中删除重复的行。
答案 0 :(得分:0)
如果同一密钥有多个行,则应该找出导致问题的原因以及解决方法。
对于重复的行,简单的SELECT DISTINCT就足够了。
如果只有重复的键,则可以进行汇总(最大值/总和/平均),但是在大多数情况下,您必须确定哪些行是最新,例如
USING (SELECT SWTBAJAF
,NUMCLIENTE
,CSI
,CTR_NUMCSIR
,CTE_NUMREL
,CTR_TIPOREL
,CTR_NUMNOM
,CTR_TIPPAR
,CTR_PORCENT
,CTR_PUESTO
,CTE_FEALTA_2K
,CTE_FEMODI_2K
,FECINF
FROM ${DTTK_COMMON_default_database}.LA1DB51CTERELCTE_STG
QUALIFY
ROW_NUMBER()
OVER(PARTITION BY NUMCLIENTE, CSI, CTR_NUMCSIR, CTE_NUMREL, CTR_TIPOREL
ORDER BY ??? DESC) = 1 -- any timestamp/sequence to find the best match?
) AS SRC