如何使用配置单元将表中的列值转换为行值

gwo2fgha  于 2021-06-24  发布在  Hive
关注(0)|答案(2)|浏览(397)

例如:当前表格

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中的列转换为行时,应忽略位置列上的空值。位置为空值的雇员id和姓名不应带到目标表中。3.当employee\u id和employee\u name作为重复值时,只应将一个值带到目标表中

cbwuti44

cbwuti441#

一个简单的选项使用 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

使用Map和 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
nhaq1z21

nhaq1z212#

可能最简单的方法就是 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;

相关问题