Hive的in中不能子查詢,in中的多字段子查詢

2021-10-09 13:25:48 字數 2053 閱讀 5426

error:semanticexception [error 10249]: line 1:146 unsupported subquery expression 'userid': correlating expression cannot contain unqualified column references.

hive查詢中,這個異常想必大家經常會遇到,在hive中,in、not in 是不支援子查詢的,今天來看看替代的方法

我們模擬的需求,從a表查詢的時候,需要在結果中過濾掉b表中的userid。

上sql

select distinct userid from table_a as a where a.dt >= '20200209' and a.userid 

not in (select distinct userid from table_b as b where b.dt >= '20200209');

這是我們的正常邏輯,使用not in 過濾掉b表中的userid。但是hive目前,in、not in 是不支援子查詢的,就會報我們開始提到的那個錯誤:

error:semanticexception [error 10249]: line 1:146 unsupported subquery expression 'userid': correlating expression cannot contain unqualified column references.,我們來看看代替方法。

使用jion改寫,應該是我們最先想到的一種方式,至於連線的方式,應該根據具體的需求具體分析吧,這裡使用left jion示例一下,左聯以後,加上b表userid為空的條件,就可以實現我們的需求

select distinct a.userid from table_a as  a left join  table_b as b

on a.userid=b.userid

where b.userid is null;

其實我們還可以使用exists進行改寫,先上sql

select distinct a.userid from table_a as a where a.dt >= '20200209' 

and not exists

(select distinct b.userid from table_b as b where b.dt >= '20200209' and a.userid = b.userid);

exists的語法有時間給大家講一下,這裡就注意幾點。

where後面使用not exists 時候,不需要跟著字段

not in寫法

where userid not in ()

not exists寫法

where userid not exists ()

使用了 not exists 後面就可以跟乙個子查詢,而過濾條件,文中是根據userid過濾的,所以這個通過userid的條件寫到了子查詢的where條件裡面去了。

子查詢的過濾條件

這種寫法就相當於join中的on。i

ps:在in中的多字段子查詢

select * from tab1 t1

where t1.col1 in (select col1 from tab2)

and t1.col2 in (select col2 from tab2);

現在分享一種簡便的方法給大家:

select * from tab  t

where (t.col1,t.col2) in (select col1,col2 from tab2);

子查詢中不能包含order by

今天做開發遇到乙個問題 寫sql語句 select top 20 from select top 200 from ump user order by id ttnn sql查不出來 因為子查詢中不能包含order by 子句 具體原因 在8i以前,子查詢中不能包含order by子句。從8i開始,...

關於hive中not in不支援子查詢的解決方法

表資訊 使用者user表 userid,name 黑名單banuser表 userid,name 問題 找出不在黑名單中的使用者資訊 userid,name hive暫時不支援以下查詢方式 select aa.from user aa where aa.user not in select a.us...

從HIVE中中查詢

從hive資料庫查詢文件 by ymd 拼接sql語句 string sql select from doc file where contains name wildcard 拼接名稱查詢語句 if stringutils.isnoneempty unstructuredbean.getname ...