Дубликаты вызывают ошибку, давайте получим уникальную запись для каждого 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