我需要用另一个表的值更新表(表中有重复项)中的列。我尝试了几对代码,但这给了我错误
错误:更新已取消:尝试使用值更新目标行 来自多个联接行
这是我的代码:
UPDATE SERGIU_BI_CCM_AGG_MTH t1
SET t1.CURRENT_SEC = foo.CURRENT_SEC
FROM (
SELECT t1a.sub_id, t1a.CURRENT_BRAND, t2.CURRENT_SEC, t2.from_date,
t2.to_date
FROM SERGIU_BI_CCM_AGG_MTH t1a
LEFT JOIN BI_CCM_BASE t2
ON t1a.sub_id = t2.sub_id and t1a.CURRENT_BRAND = t2.CURRENT_BRAND
)
foo
WHERE t1.sub_id = foo.sub_id and t1.CURRENT_BRAND = foo.CURRENT_BRAND
and t1.agg_mth between to_char(foo.from_date,'YYYYMM') and
to_char(foo.to_date-1,'YYYYMM');
有人可以帮助我吗?
答案 0 :(得分:0)
我认为您可以使用以下两种方式:
UPDATE SERGIU_BI_CCM_AGG_MTH t1
LEFT JOIN BI_CCM_BASE t2
ON t1.sub_id = t2.sub_id and t1.CURRENT_BRAND = t2.CURRENT_BRAND
SET t1.CURRENT_SEC = t2.CURRENT_SEC
WHERE t1.agg_mth between to_char(t2.from_date,'YYYYMM') and
to_char(t2.to_date-1,'YYYYMM');
UPDATE SERGIU_BI_CCM_AGG_MTH t1, BI_CCM_BASE t2
SET t1.CURRENT_SEC = t2.CURRENT_SEC
WHERE t1.sub_id = t2.sub_id and t1.CURRENT_BRAND = t2.CURRENT_BRAND
and t1.agg_mth between to_char(t2.from_date,'YYYYMM') and
to_char(t2.to_date-1,'YYYYMM');
希望这会有所帮助! :)
答案 1 :(得分:0)
重复项引起错误,让我们使用sub_id
获得每个CURRENT_BRAND
和row_number()
的唯一记录。
尝试以下查询
UPDATE SERGIU_BI_CCM_AGG_MTH t1
SET t1.CURRENT_SEC = foo.CURRENT_SEC
FROM (
SELECT t1a.sub_id
, t1a.CURRENT_BRAND
, t2.CURRENT_SEC
, t2.from_date
, t2.to_date
, row_number() over (partition by t1a.sub_id,CURRENT_BRAND order by from_date desc) as rn
FROM SERGIU_BI_CCM_AGG_MTH t1a
LEFT JOIN BI_CCM_BASE t2 ON t1a.sub_id = t2.sub_id
and t1a.CURRENT_BRAND = t2.CURRENT_BRAND
)foo
WHERE t1.sub_id = foo.sub_id
and t1.CURRENT_BRAND = foo.CURRENT_BRAND
and t1.agg_mth between to_char(foo.from_date,'YYYYMM') and to_char(foo.to_date-1,'YYYYMM');
and foo.rn = 1
答案 2 :(得分:0)
您需要以下查询。
UPDATE SERGIU_BI_CCM_AGG_MTH t1
SET t1.CURRENT_SEC = (SELECT t2.CURRENT_SEC
FROM SERGIU_BI_CCM_AGG_MTH t1a
LEFT JOIN BI_CCM_BASE t2
ON t1a.sub_id = t2.sub_id and t1a.CURRENT_BRAND = t2.CURRENT_BRAND
WHERE t1.sub_id = foo.sub_id and t1.CURRENT_BRAND = foo.CURRENT_BRAND
and t1.agg_mth between to_char(foo.from_date,'YYYYMM') and
to_char(foo.to_date-1,'YYYYMM'))