如何删除雪花数据库表中的重复记录 [英] How to delete Duplicate records in snowflake database table
本文介绍了如何删除雪花数据库表中的重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何删除雪花表中的重复记录。谢谢
ID Name
1 Apple
1 Apple
2 Apple
3 Orange
3 Orange
结果应为:
ID Name
1 Apple
2 Apple
3 Orange
推荐答案
在此添加不会重新创建表的解决方案。这是因为重新创建表可能会中断大量现有配置和历史记录。
相反,我们将只删除重复的行,并在事务中插入每个行的单个副本:
-- find all duplicates
create or replace transient table duplicate_holder as (
select $1, $2, $3
from some_table
group by 1,2,3
having count(*)>1
);
-- time to use a transaction to insert and delete
begin transaction;
-- delete duplicates
delete from some_table a
using duplicate_holder b
where (a.$1,a.$2,a.$3)=(b.$1,b.$2,b.$3);
-- insert single copy
insert into some_table
select *
from duplicate_holder;
-- we are done
commit;
优点:
- 不重新创建表
- 不修改原始表
- 仅删除和插入重复行(有利于降低时间旅行存储成本,避免不必要的重新群集)
- 全部在一个事务中
这篇关于如何删除雪花数据库表中的重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文