【问题标题】:How do i convert column values in a table to row values using hive如何使用 hive 将表中的列值转换为行值
【发布时间】:2020-05-19 19:07:41
【问题描述】:

例如: 当前表

employee_id employee_name   location_1  location_2  location_3
111 Reeta   Delhi
112 Pinky   Chennai Kerala  Null
113 Tinku   Noida   Ranchi  Jaipur
114 Teena   Null
115 Bing    Assam   Assam

想要的表:

employee_id employee_name   Location
111 Delhi
112 Pinky   Chennai
112 Pinky   Kerala
113 Tinku   Noida
113 Tinku   Ranchi
113 Tinku   Jaipur
115 Bing    Assam

1.在目标表中将列转换为行时,应忽略位置列上的空值 2. 以空值作为位置的employee_id 和name 不应该被带到目标表中。 3.当emplyee_id和employee_name为重复值时,只应将一个带入目标表

【问题讨论】:

    标签: sql hive pivot explode unpivot


    【解决方案1】:

    一个简单的选项使用union all

    select employee_id, employee_name, location_1 location from mytable where location_1 is not null
    union all 
    select employee_id, employee_name, location_2 from mytable where location_2 is not null
    union all 
    select employee_id, employee_name, location_3 from mytable where location_3 is not null
    

    使用地图和lateral view explode 的特定于配置单元的方法可能更有效:

    select employee_id, employee_name, location
    from (
        select 
            employee_id, 
            employee_name, 
            map("location_1", location_1, "location_2", location_2, "location_3", location_3) as mp
        from mytable
    ) t
    lateral view explode(mp) m as locname, location  
    where location is not null
    

    【讨论】:

      【解决方案2】:

      可能最简单的方法是union all。但这需要多次扫描表格。所以,改为:

      select tn.*
      from (select t.employee_id, t.employee_name,
                   (case when n.n = 1 then location_1
                         when n.n = 2 then location_2
                         when n.n = 3 then location_3
                    end) as location 
            from t cross join
                 (select 1 as n union all select 2 union all select 3) n
           ) tn
      where location is not null;
      

      【讨论】:

        猜你喜欢
        • 2021-12-19
        • 1970-01-01
        • 2018-12-19
        • 1970-01-01
        • 1970-01-01
        • 2020-11-09
        • 1970-01-01
        • 2016-11-09
        相关资源
        最近更新 更多