使用其他列表的值更新列表(表重复)

问题描述 投票:0回答:2

我需要用另一个表的值更新表(表中有重复项)中的列。我尝试了几个代码,但它给了我错误

错误:更新已取消:尝试使用多个联接行中的值更新目标行

谁能帮我

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');
sql netezza
2个回答
0
投票

我认为您可以使用以下两种方式:

  • 带有“ join”

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');
  • 没有“ join”:
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');

希望这会有所帮助! :)


0
投票

重复项导致错误,让我们使用sub_id获得每个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 sub_id order by from_date) 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
© www.soinside.com 2019 - 2024. All rights reserved.