2

我使用的是雪花计算提供的雪花弹性数据仓库。我有一个没有标识或主键列的表,但整个表中有重复的行。

我的问题是,在每个重复集中,我需要通过将表中的finalsaleandtaxindollars列清零来更新每个重复集中除了一个重复行之外的所有重复行。哪个重复行保持不变/不更新都没有关系。因此,如果有 3 条重复的线,则只需将其中 2 条线清零,而剩下的另一条线应保持不变。我尝试执行以下查询,该查询在此处作为答案给出:

更新 SQL Server 表中除一条重复记录外的所有重复记录

但它在雪花中不起作用。它告诉我对象“T”不存在。然而,下面返回需要更新的确切行(为每个不更新的集合留出 1 个行项)。关于如何在 Snowflake 中完成此操作并获取除 1 之外的所有重复行的任何想法,每个重复集都更新为 0finalsaletaxindollars

UPDATE t SET
    t.finalsale = 0,
    t.taxindollars = 0
FROM (
    SELECT *, row_number() OVER(PARTITION BY 
      saleid,
      locationname,
      customertype,
      finalsale,
      quantity,
      sku                                
    ORDER BY 
        (SELECT NULL)) row_num  
    FROM 
        salesdetail
) t 
WHERE row_num > 1

谢谢你的帮助!

4

2 回答 2

0

使用 ROW_NUMBER Partition By Clause 并将结果放入 CTE。它可能看起来像:

 with myData as (
  select mycolumn1, mycolumn2, myDate
    ,row_number() over (partition by mycolumn1 order by myDate desc) as priority
  from source_table
)
select * from myData where priority = 1

然后对数字 = 1 的行运行选择或更新

于 2019-12-11T17:24:14.603 回答
0
--drop table foo purge;
create table foo as 
          select 1 id, 'x' dsc from dual
union all select 1 id, 'x' dsc from dual
union all select 1 id, 'x' dsc from dual
union all select 1 id, 'z' dsc from dual
union all select 1 id, 'z' dsc from dual
union all select 1 id, 'z' dsc from dual
union all select 2 id, 'y' dsc from dual
union all select 2 id, 'y' dsc from dual
union all select 2 id, 'y' dsc from dual;

select * from foo;

create table bar as 
with trg as (select ID
                , DSC
                , row_number() over (partition by ID, DSC order by null) rn
         from foo) 
select ID
       , case when rn = 1 then upper(dsc)
              else dsc
         end DSC
from trg;

truncate table foo;
insert into foo select * from bar;
commit;
drop table bar purge;

select * from foo;
于 2017-08-04T15:02:33.403 回答