从不工作的表中删除重复记录

hrirmatl  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(317)

我正在尝试从红移数据库中删除重复项

WITH CTE AS
         (SELECT org_country_code,dest_country_code,postcode,
                 zone,kg,value,carrier,version,
                 ROW_NUMBER() OVER (
                     partition BY org_country_code, dest_country_code, postcode,
                         zone, kg, value,carrier, version
                     ORDER BY org_country_code, dest_country_code,
                         postcode, zone, kg, value,carrier, version) rnum
          FROM dhl_gb.dhl_gb_td_exp_ww_l10kg_transformed
             ) DELETE FROM CTE WHERE rnum<>1

这里面怎么了?有什么需要帮忙的吗?

5cnsuln7

5cnsuln71#

更新此项在红移中不起作用。
如果没有pk,则需要使用ctid:

WITH CTE AS (
  SELECT CTID as rid,
         ROW_NUMBER() OVER (
                     partition BY org_country_code, dest_country_code, postcode,
                         zone, kg, value,carrier, version
                     ) rnum
    FROM dhl_gb.dhl_gb_td_exp_ww_l10kg_transformed
) 
DELETE FROM dhl_gb.dhl_gb_td_exp_ww_l10kg_transformed
 WHERE CTID in (select rid from CTE where rnum > 1);
owfi6suc

owfi6suc2#

不确定是否可以修改 CTE . 这个怎么样

WITH CTE AS
         (SELECT org_country_code,dest_country_code,postcode,
                 zone,kg,value,carrier,version,
                 ROW_NUMBER() OVER (
                     partition BY org_country_code, dest_country_code, postcode,
                         zone, kg, value,carrier, version
                     ORDER BY org_country_code, dest_country_code,
                         postcode, zone, kg, value,carrier, version) rnum
          FROM dhl_gb.dhl_gb_td_exp_ww_l10kg_transformed
             ), 

CTE2 AS SELECT * FROM CTE WHERE rnum = 1

SELECT * FROM CTE2

相关问题