【问题标题】:Getting set difference with duplicate rows from Hive tables从 Hive 表中获取与重复行的差异
【发布时间】:2020-03-31 11:06:55
【问题描述】:

我有两个 Hive 表:Table1、Table2。 Table1 有重复的行,Table2 没有。我想从 Table1 中获取 Table2 中不存在的缺失数据,包括重复项。如何在 Hive 查询语言中完成这项工作?

例子:

表1数据:

Col1,Col2
A1,V1
A1,V1
A2,V2
A3,V3
A3,V3
A3,V3
A4,V4

表2数据:

Col1,Col2
A1,V1
A2,V2
A3,V3

我想从 Table1 中获取以下缺失数据:

Col1,Col2
A1,V1
A3,V3
A3,V3
A4,V4

【问题讨论】:

    标签: hive hiveql


    【解决方案1】:

    你可以使用类似的东西:

    with t1 as (
      select 'A1' col1,'V1' col2 union all
      select 'A1' col1,'V1' col2 union all
      select 'A2' col1,'V2' col2 union all
      select 'A3' col1,'V3' col2 union all
      select 'A3' col1,'V3' col2 union all
      select 'A3' col1,'V3' col2 union all
      select 'A4' col1,'V4' col2
    ),
    t2 as (
      select 'A1' col1,'V1' col2 union all
      select 'A2' col1,'V2' col2 union all
      select 'A3' col1,'V3' col2
    ),
    t1_with_rn as (
      select t1.*, row_number() over(partition by t1.col1, t1.col2) rn from t1
    )
    select 
      t1_with_rn.col1, t1_with_rn.col2
    from 
      t1_with_rn
      left join t2 on (t1_with_rn.col1 = t2.col1 and t1_with_rn.col2 = t2.col2 and t1_with_rn.rn = 1)
    where
      t2.col1 is null and t2.col2 is null
    

    【讨论】:

    • 谢谢,这行得通。但是,我的表有数百万行。那么,让我们看看它的表现如何。
    猜你喜欢
    • 2015-09-12
    • 2021-11-06
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 2015-03-26
    • 1970-01-01
    相关资源
    最近更新 更多