配置单元扫描和 select 在一个查询中

hive scan and select in one query

我有一个蜂巢 table,说 emp_details(name, dept)

在此 table 中,我需要检查是否存在 dept = ‘a’ 的记录,然后 select 这些记录。如果找不到这样的记录,那么只有我会选择 dept = ‘b’ 的记录。源数据有 'a''b' 作为 dept 值,我的结果集将包含 'a''b' 而不是两者。

问题是我只能使用一个配置单元查询来解决这个问题。

计算a_exist标志并将其用于过滤:

    select name, dept 
       from 
           (select name, 
                   dept, 
                   (count(case when dept='a' then 1 end) over()>0) as a_exist 
             from test_a 
           )a 
      where (a_exist and dept='a')            --only a if exists 
         or ((NOT a_exist)and dept='b')       --return b if a not exists
;