【问题标题】:lateral view explode in bigquery横向视图在bigquery中爆炸
【发布时间】:2022-01-26 09:24:24
【问题描述】:

我想使用 BigQuery 做类似的事情。

输入表

Col1 Col2 Col3 Col4
1 A,B,C 123 789

输出表

ID COL VALUE
1 COL1 1
1 COL2 A,B,C
1 COL3 123
1 COL4 789

我在 Hive 中使用 LATERAL VIEW explode(MAP) 得到了这个,但在 bigquery 中我不能得到同样的结果。

【问题讨论】:

    标签: sql hive google-bigquery


    【解决方案1】:

    考虑以下方法

    select id, col, value
    from (select *, row_number() over() as id from your_table)
    unpivot (value for col in (Col1, Col2, Col3, Col4))       
    

    f 适用于您问题中的示例数据

    with your_table as (
      select '1' Col1, 'A,B,C' Col2, '123' Col3, '789' Col4
    )
    

    输出是

    注意 - 这种特殊方法要求所有列 (Col1 - Col4) 的类型相同。如果不是这种情况,您将需要首先为其中一些应用强制转换以使其成为字符串

    【讨论】:

    • 比工会更有效率吗?如果 biquery 支持动态或使用通配符,那肯定会改变游戏规则。
    【解决方案2】:

    如果是离散的列数,您可以为此使用 UNION...

    select id, 'Col1' as Column, col1 as Value
    from table
    union all
    select id, 'Col2' as Column, col2 as Value
    from table
    union all
    select id, 'Col3' as Column, col3 as Value
    from table
    

    【讨论】:

      猜你喜欢
      • 2017-07-13
      • 2023-03-06
      • 2018-12-21
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 2017-04-04
      • 2019-03-17
      • 2016-11-20
      相关资源
      最近更新 更多