【问题标题】:BigQuery - Remove specific duplicate recordsBigQuery - 删除特定的重复记录
【发布时间】:2015-10-27 20:04:39
【问题描述】:

我有一个 BigQuery 表,其中包含如下数据:

date  hits_eventInfo_Category  hits_eventInfo_Action  session_id  user_id  hits_time  hits_eventInfo_Label

20151021  Air  Search  1445001  A232  1952  CurrentLocation
20151021  Air  Search  1445001  A232  1952  CurrentLocation
20151021  Air  Search  1445001  A232  1952  CurrentLocation
20151021  Air  Select  1445001  A232  7380  Vendor
20151021  Air  Select  1445001  A232  7380  Vendor
20151021  Air  Select  1445001  A232  7380  Vendor

如您所见,有一系列重复记录。我希望得到每个重复记录集中的重复记录之一。例如:

date  hits_eventInfo_Category  hits_eventInfo_Action  session_id  user_id  hits_time  hits_eventInfo_Label

20151021  Air  Search  1445001  A232  1952  CurrentLocation
20151021  Air  Select  1445001  A232  7380  Vendor

我该怎么做?

提前致谢!

【问题讨论】:

    标签: sql duplicates google-bigquery


    【解决方案1】:

    您可以使用 DISTINCT 子句,也可以对数据进行分组。这些会将返回的数据汇总到每个唯一条目的一行中。

    SELECT DISTINCT [date], [hits_eventInfo_Category], [hits_eventInfo_Action], [session_id], [user_id], [hits_time], [hits_eventInfo_Label]
    FROM [BigQuery]
    
    --OR
    
    SELECT [date], [hits_eventInfo_Category], [hits_eventInfo_Action], [session_id], [user_id], [hits_time], [hits_eventInfo_Label]
    FROM [BigQuery]
    GROUP BY [date], [hits_eventInfo_Category], [hits_eventInfo_Action], [session_id], [user_id], [hits_time], [hits_eventInfo_Label]
    

    注意:这不会删除您的重复数据,只是不会显示在您的 select 语句的结果中。如果您希望永久删除重复条目,请使用 @singhsac 的响应,利用窗口函数。

    【讨论】:

      【解决方案2】:

      您可以对重复项进行分组。保留一行,从重复组中删除剩余的行:

      试试这个(我假设表名和其他字段)

      ;WITH rmvDuplicate
              AS (SELECT  ROW_NUMBER() OVER (PARTITION BY [date],  [hits_eventInfo_Category],  [hits_eventInfo_Action],  [session_id],  [user_id],  [hits_time],  [hits_eventInfo_Label]
                                              ORDER BY (SELECT 0)) dup
                  FROM    BigQuery_table)
          DELETE FROM rmvDuplicate
          WHERE   dup > 1
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 2012-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      相关资源
      最近更新 更多