【问题标题】:Removing the duplicates from the table in the snowflake database based on certain filter criteria根据某些过滤条件从雪花数据库中的表中删除重复项
【发布时间】:2021-09-06 16:00:17
【问题描述】:

我在雪花数据库中有一个示例表,其中包含有关购买的产品的详细信息、商店的代码、购买产品的日期、产品的数量和业务类型,如下所示:

从此表中,我需要根据“Shop_Code”和“Purchase_Date”列删除重复项。在删除重复项时,我还需要记住以下条件。它们是:

  1. 如果我们在“Shop_Code”和“Purchase_Date”分组时得到两行,其中“Business_Type”都是“Large”,那么可以删除任何一行。
  2. 如果我们在“Shop_Code”和“Purchase_Date”分组时得到两行,其中“Business_Type”都是“MidSmall”,那么可以删除任何一行。
  3. 如果我们在对“Shop_Code”和“Purchase_Date”进行分组时,将“Business_Type”作为“Large”的另一行作为“MidSmall”,那么在这种情况下,“Business_Type”作为“MidSmall”的行应该删除保留“大”作为业务类型的行。

我期望的输出是第 1、3、5 和 8 行应该出现在输出表中。请求您在 Snowflake SQL 方面的帮助。

【问题讨论】:

  • 用户 RANK()/DENSE_RANK() 并从中删除要删除的列,在此列的顶部,应用 Row_NUMBER() 转换,这将帮助您删除所需的行。
  • @akshindesnowflake 你能详细说明我给出的表格吗?
  • 您能否提供您的 SQL 表定义和此示例数据脚本。
  • @akshindesnowflake 表定义 Product_Bought - VARCHAR Shop_Code - VARCHAR Business_Type - VARCHAR Purchase_Date - DATE Amount - NUMBER 并且我正在尝试的查询是 With Dup as ( select "Shop_Code", "Purchase_Date", "Business_Type", row_number() over (partition by "Shop_Code","Purchase_Date" ORDER BY "Shop_Code","Purchase_Date" Desc,"Business_Type" Asc) as rn from purchase_table ) Select * from dup where rn > 1; 但我无法应用提到的条件在我的描述中。

标签: sql snowflake-cloud-data-platform


【解决方案1】:

您不能使用 Snowflake 直接从表中删除。您所涉及的选项涉及运行select 查询以识别重复项并将其插入到原始表中,如下所示:

设置样本表:

create or replace table sample_table as (
    select
        column1::varchar as product_bought,
        column2::varchar as shop_code,
        column3::varchar as business_type,
        column4::date    as purchase_date,
        column5::number  as amount
    from
    values ('Mobile', 'AVR', 'MidSmall', '2012-09-01', 13000),
           ('Mobile', 'AVR', 'MidSmall', '2012-09-01', 13000),
           ('Tablet', 'SVU', 'Large', '2012-02-23', 16000),
           ('Tablet', 'SVU', 'MidSmall', '2012-02-23', 16000),
           ('Laptop', 'MNR', 'Large', '2015-09-22', 78000),
           ('Laptop', 'MNR', 'Large', '2015-09-22', 78000),
           ('Headset', 'NZL', 'MidSmall', '2018-08-14', 1200),
           ('Headset', 'NZL', 'Large', '2018-08-14', 1200)
);

创建一个包含非重复记录的新表:

create or replace table sample_table_non_duplicates as
select
    st.*
from sample_table st
    qualify row_number() over (partition by shop_code, purchase_date order by IFF(upper(business_type) = 'MIDSMALL', 2, 1) ) = 1
;

将原始表与不包含重复的表交换:

alter table sample_table swap with sample_table_non_duplicates;
-- drop table sample_table_non_duplicates --(Drops original table!)

结果:

select * from sample_table;
+--------------+---------+-------------+-------------+------+
|PRODUCT_BOUGHT|SHOP_CODE|BUSINESS_TYPE|PURCHASE_DATE|AMOUNT|
+--------------+---------+-------------+-------------+------+
|Mobile        |AVR      |MidSmall     |2012-09-01   |13000 |
|Headset       |NZL      |Large        |2018-08-14   |1200  |
|Laptop        |MNR      |Large        |2015-09-22   |78000 |
|Tablet        |SVU      |Large        |2012-02-23   |16000 |
+--------------+---------+-------------+-------------+------+

可以在原始表上运行insert overwrite 查询,该查询会删除重复项,但这可能有点风险,并且您可能会丢失原始数据。 Here are some other options

【讨论】:

  • 感谢西蒙的帮助
猜你喜欢
  • 1970-01-01
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
  • 2021-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多