使用HIVE LATERAL VIEW选择不存在的数据为null

时间:2017-09-20 21:04:41

标签: hive hiveql

我试图在hive中使用外部爆炸将非现有数据作为null,但我的查询没有返回任何内容。 编辑: 表 - 年字符串,companyrank

$('btn btn-warning btn-block').on('click', function(){
if($zip !=0 && $fName !=0 ){
$('input#zip').value(valueZipAdditional);
$('btn btn-warning btn-block').click();     
}
});

示例数据 编辑:

year:string,topcompanies:array<struct<name:string,rank:string>>

查询获取数据

    2015,
  "topcompanies":[
  {"name":"apple","rank":"1"},
  {"name":"samsung","rank":"2"},
  {"name":"SONY","rank":"3"},
  ]

  2016,
  "topcompanies":[
  {"name":"apple","rank":"1"},
  {"name":"samsung","rank":"2"},
  {"name":"SONY","rank":"3"},
  {"name":"LG","rank":"4"}
  ]

预期产出 -

select  year, rank1, rank2, rank3, rank4
FROM companyrank
LATERAL VIEW outer explode(topcompanies) rank1_t as rank1_v
LATERAL VIEW outer explode(topcompanies) rank2_t as rank2_v
LATERAL VIEW outer explode(topcompanies) rank3_t as rank3_v
LATERAL VIEW outer explode(topcompanies) rank4_t as rank4_v
WHERE 
 (rank1_v.rank = 1 or rank1_v.rank is null)
 AND (rank2_v.rank = 2 or rank2_v.rank is null)
 AND (rank3_v.rank = 3 or rank3_v.rank is null)
 AND (rank4_v.rank = 4 or rank4_v.rank is null)

如果存在rank4数据

expected output when rank4 does not exists
year  rank1 rank2   rank3   rank4
2016  apple samsung SONY    null

编辑:

我需要每年获得所有4个等级,如果任何等级不存在,则等级应显示为NULL。

1 个答案:

答案 0 :(得分:0)

您问题的直截了当的答案是“使用lateral view outer”,但有一个更清晰的解决方案。

select      min (case when i.rank = 1 then i.name end)  as rank1
           ,min (case when i.rank = 2 then i.name end)  as rank2
           ,min (case when i.rank = 3 then i.name end)  as rank3
           ,min (case when i.rank = 4 then i.name end)  as rank4

from        companyrank c
            lateral view inline(topcompanies) i
;
+--------+----------+--------+--------+
| rank1  |  rank2   | rank3  | rank4  |
+--------+----------+--------+--------+
| apple  | samsung  | SONY   | NULL   |
+--------+----------+--------+--------+