【问题标题】:Deduplicating rows in BigQuery based on condition根据条件对 BigQuery 中的行进行重复数据删除
【发布时间】:2020-03-02 13:04:14
【问题描述】:

我目前正在尝试对我的一张表在 Google BigQuery 中的行进行重复数据删除。基本上,我有一个表,其中包含除一列之外的重复值的基本行。一个例子是:

请注意,除“广告组名称”列外,所有列都是相同的。我想做的是:如果所有列相同但列广告组名称不同,则保留单行(哪一行无关紧要) >.

我正在考虑创建分区并使用排名函数来表示该分区内的不同值。比如:

RANK() OVER (PARTITION BY Adgroup ID, date, Sales, Cost ORDER BY Ad group name) AS rank

理论上(呵呵)这应该会导致:

使用它,我可以使用WHERE Rank = 1 过滤一个新的子查询。在这种情况下,这将删除所有重复的行。

但是,我发现 BigQuery 不支持使用 FLOAT64 进行分区,因此我的解决方案不起作用。另外,我认为有更好的方法可以做到这一点,但我很难找到如何做到这一点。我可以专门利用 BigQuery 中的哪个功能?

【问题讨论】:

    标签: sql google-bigquery


    【解决方案1】:

    您可以为此使用group by

    select Adgroup ID, date, Sales, Cost, any_value(ad_group_name)
    from t
    group by Adgroup ID, date, Sales, Cost;
    

    【讨论】:

      【解决方案2】:

      对于这种情况有多种可能的解决方案。 一个使用partition byanalytic function,另一个使用group byARRAY_AGG()

      (避免GROUP BYany_value 的可能解决方案)

      1. 分析功能ROW_NUMBER
      WITH
      org_table AS (
          SELECT 15840 as AdGroupID, '22-1-2019' as AdDate, 'TVs' as AdGroupName, 800 as Sales, 200 as Cost
          UNION ALL SELECT 15840 as AdGroupID, '22-1-2019' as AdDate, 'Televisions' as AdGroupName, 800 as Sales, 200 as Cost
      )
      SELECT *
      FROM org_table
      WHERE TRUE
      QUALIFY ROW_NUMBER() OVER (PARTITION BY AdgroupID ORDER BY AdDate DESC) = 1
      ;
      
      1. GROUP BYAGG_ARRAY
      WITH
      org_table AS (
          SELECT 15840 as AdGroupID, '22-1-2019' as AdDate, 'TVs' as AdGroupName, 800 as Sales, 200 as Cost
          UNION ALL SELECT 15840 as AdGroupID, '22-1-2019' as AdDate, 'Televisions' as AdGroupName, 800 as Sales, 200 as Cost
      )
      SELECT
          AdgroupID,  -- primary key
          ARRAY_AGG(
              STRUCT(AdDate, AdGroupName, Sales, Cost)
              ORDER BY AdGroupName ASC LIMIT 1
          )[OFFSET(0)].*
      FROM org_table
      GROUP BY AdgroupID
      ;
      

      【讨论】:

        猜你喜欢
        • 2020-01-13
        • 2021-04-02
        • 2017-11-23
        • 1970-01-01
        • 1970-01-01
        • 2020-05-26
        • 2017-12-08
        • 2017-10-04
        • 2021-12-20
        相关资源
        最近更新 更多