使用exists
代替:
select e.name, e.job_id, e.sal
from emp e
where exists (select 1
from dept d join
location L
on d.location_id = L.location_id
where l.city = 'US' and d.dept_id = e.dept_id
);
您可以參考documentation,它涵蓋了WHERE
子句中的子查詢。
此查詢似乎回答了這樣一個問題:在美國擁有地點的部門中,員工的工作是什麼。你也可以在FROM
子句中用子查詢來做到這一點;
select e.name, e.job_id, e.sal
from emp e join
(select distinct d.dept_id
from dept d join
location L
on d.location_id = L.location_id
where l.city = 'US'
) d
on d.dept_id = e.dept_id;
我應該注意到,雖然,「美」不是通常被認爲是一個城市。
編輯:
顯然,如果一個部門只能有一個位置,然後在「半聯接」是沒有必要的。 SELECT DISTINCT
只能是SELECT
。 。 。或者,您可以像Dudu的回答一樣使用JOIN
。無論如何,EXISTS
將工作。在許多數據庫中,它會有很好的表現(有時是最好的表現);我不確定Hive中的性能影響。
你還在嗎? –