我正在使用Snowflake Computing提供的Snowflake弹性数据仓库。我有一个没有标识或主键列的表,但整个表中有重复的行。
我的问题是,在每个重复的集中,我需要通过清零表中的finalsale
和taxindollars
列来更新每个重复集中除一行之外的所有重复行。重复的行中的哪一个保持不变/未更新并不重要。因此,如果有 3 行重复,则只需要清零其中的 2 行,其余另一行应保持不变。我尝试执行以下查询,该查询在此处作为答案给出:
更新 SQL Server 中表中除一条重复记录之外的所有重复记录
但它在雪花中不起作用。它告诉我对象"T"不存在。但是,下面返回需要更新的确切行(为每个不更新的集合省略 1 个行项目(。关于如何在 Snowflake 中完成此操作并为每个重复集更新 0 的 1 行以外的所有重复行的任何想法finalsale
和taxindollars
?
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
感谢您的帮助!
--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;
使用 ROW_NUMBER 分区依据子句并将结果放入 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 的行运行选择或更新