如果我的 where 条件使用 Hive 给出空输出,如何显示表中的所有记录



当我的where条件返回空输出时显示表的所有记录

EX:
Data:
x,y
1,2
2,3
3,4
select * from table where x=1 ;
output
x,y
1,2
select * from table where x=10 ;

通常它给我一个空输出,而不是它应该在表中显示整个记录。

Output
x,y
1,2
2,3
3,4

在这种情况下,提供的值不会出现在 x 数据中,因此,它应该显示表中的所有值

一种方法是:

with t as (
select *
from table
where x = 1 
)
select t.*
from t
union all
select t2.*
from table t2
where not exists (select 1 from t);

如果x是唯一的,另一种有趣的方法有效:

select t.*
from table t left join
table t2
on t2.x = 1 
where (t2.x = 1 and t.x = 1) or (t2.x is null);

最新更新