如何克服Hive中的子查询CASE语句

时间:2018-04-28 05:44:19

标签: hive hiveql hadoop2

我必须将列值填充到' Y'如果条件为真,否则“N”#。但是,如果在hive中它不支持子查询,那么如何在HIVE中返回。

(case
  when exists
  (select 1
  from fntable fs
  join dfntable dfs
  on fs.id = dfs.id
  and dfs.datetime = 
     (select max (cd.datetime)
      from dfntable cd group by id)
  and fs.s_id = dfs.s_id)  then 'Y'
else 'N')"

1 个答案:

答案 0 :(得分:1)

使用具有分析函数的子查询+左连接。加入后,' Y':

select case when cd.id is not null then 'Y' else 'N' end 
  from fntable fs
      left join
      ( --group by  cd.id, cd.s_id and filter 
       select cd.id, cd.s_id 
         from
            ( select max (cd.datetime) over (partition by dc.id) as max_id_datetime,
                     cd.id, cd.s_id, cd.datetime
                from dfntable cd
            ) cd
        where cd.datetime=max_id_datetime --filter only records with max date
        group by cd.id, cd.s_id
      ) cd on fs.id = dfs.id and fs.s_id = dfs.s_id