【问题标题】:Hive Divide single Integer column into multiple rowsHive 将单个 Integer 列分成多行
【发布时间】:2021-10-29 02:14:44
【问题描述】:

我在 Hive 中有 3 列的表,我想根据特定值划分最后一列(在我的情况下为 200) Hive 表结构:-

ID ,Name ,Value 
1,"Jan",800
2,"Mar",200
3,"DEC",350
4,"APR",400

我希望输出为

ID ,Name ,Value 
1,"Jan",200
1,"Jan",200
1,"Jan",200
1,"Jan",200
2,"Mar",200
3,"DEC",200
3,"DEC",150
4,"APR",200
4,"APR",200

我尝试使用侧视图爆炸,但没有给我正确的结果

【问题讨论】:

    标签: sql hive hiveql


    【解决方案1】:

    ceil(value/200) 给出为给定值生成的行数。您可以使用split(space(...)) + lateral view posexplode 生成行,然后使用posexplode返回的位置来计算当前值。

    演示:

    with mytable as (--demo dataset, use your table instaed of this CTE
    select 1 ID,"Jan" Name,800 Value union all
    select 2,"Mar",200 union all
    select 3,"DEC",350 union all
    select 4,"APR",400
    )
    
    select id, name, 
           --value 200 is hardcoded in many places, you can pass it as a parameter to the script
           case when (pos+1)*200 > value --remains less than 200
                   then value-pos*200 
                else 200 
           end as value
    from
    (
    select id, name, value, int(ceil(value / 200)) num_rows 
      from mytable
    )s  
     --generate string of space, split it and posexplode  
     lateral view outer posexplode(split(space(num_rows-1),'')) e as pos, x
    

    结果:

    id  name    value
    1   Jan      200
    1   Jan      200
    1   Jan      200
    1   Jan      200
    2   Mar      200
    3   DEC      200
    3   DEC      150
    4   APR      200
    4   APR      200
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 2019-12-23
      • 2014-07-24
      相关资源
      最近更新 更多