【问题标题】:Hive Query with collect_set使用 collect_set 进行 Hive 查询
【发布时间】:2017-03-20 06:09:03
【问题描述】:

我有 2 个表,sample_table1 有如下两列

C1  C2
001  a
001  b
001  e
002  c
002  b
003  a
003  c

sample_table2 有两列

C3  C4
a   0
b   1
c   0
d   1
e   0

我想得到像

这样的输出
F1    F2
001    1    <as 001 -> [a, b, e] -> [0, 1, 0] -> 1 (if one of the items in the collection ([a, b, e] in this case) is 1, then Column F2 should be 1 )>
002    1    <as 002 -> [c, b] -> [0, 1] -> 1>
003    0    <as 003 -> [a, c] -> [0, 0] -> 0>

我对 Hive 的内置聚合函数 collect_set 进行了很多尝试,但无法解决。我想知道是否可以在不编写任何自定义 UDF 的情况下做到这一点?

【问题讨论】:

    标签: sql hadoop hive apache-spark-sql hiveql


    【解决方案1】:

    不需要collect_set

    select      t1.c1       as f1
               ,max(t2.c4)  as f2
    
    from                sample_table1 t1
                join    sample_table2 t2
                on      t1.c2 = t2.c3
    
    group by    t1.c1      
    ;
    

    +-----+----+
    | f1  | f2 |
    +-----+----+
    | 001 |  1 |
    | 002 |  1 |
    | 003 |  0 |
    +-----+----+
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-23
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-03
      • 2011-09-20
      • 1970-01-01
      相关资源
      最近更新 更多