【问题标题】:Join hive tables : Load values of multiple row in columns of another table加入 hive 表:在另一个表的列中加载多行的值
【发布时间】:2019-01-16 04:17:51
【问题描述】:

我在 hive 中有 2 个表: 第一个表(table1):

id        val1        val2        val3
1         NULL        NULL        NULL
1         NULL        NULL        NULL
1         NULL        NULL        NULL
1         NULL        NULL        NULL

第二张表(table2):

id        val        row_number
1         x          1
1         y          2
1         z          3

我想加入表格,最终结果如下:

id        val1        val2        val3
1         x           y           z
1         x           y           z
1         x           y           z
1         x           y           z

确保在 table2 中,对于特定 id,将恰好有 3 条记录,并且每个值(列 'val')都必须加载到该 id 的所有行的 val1、val2、val3 列中在表 1 中。

【问题讨论】:

    标签: sql join hive left-join


    【解决方案1】:

    你似乎想要:

    select t1.id, t21.val as val1, t22.val as val2, t23.val as val3
    from t1 join
         t2 t21
         on t21.id = t1.id and t21.row_number = 1 join
         t2 t22
         on t22.id = t1.id and t22.row_number = 2 join
         t2 t23
         on t23.id = t1.id and t23.row_number = 3;
    

    【讨论】:

    • 嗨,戈登,感谢您的回复。我也用过类似的方法。我一直在寻找一种少于 3 个连接的更有效的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    相关资源
    最近更新 更多