使用左外连接或存在位置在hive中进行子查询

时间:2018-04-14 17:28:24

标签: hive hiveql

我试图做多个嵌套子查询。首先找到带有角色" Person"的小写名称,然后找到那些人拥有的session_ids。使用session ids列表,我想根据session ids

中的所有结果过滤原始查询
select 
U.session_id,
U.session_date,
U.email
from data.usage U
left outer join
  select 
  distinct M.session_id
  from data.usage M
  where email like '%gmail.com%'
  and data_date >= '20180101'
  and name in
    ( 
    select 
    lower(name)
    from data.users
    where role like 'Person%' 
    and isactive = TRUE
    and data_date = '20180412'
    )
on U.session_id = M.session_id

我能够让子查询自行运行,并生成session_ids列表,我尝试使用where session_id in (subqueries),但这并不是工作。我也尝试过where existsleft outer join之类的内容,但我无法让其中的任何内容发挥作用。

1 个答案:

答案 0 :(得分:1)

在您向M写入的所有查询的左外连接保留别名后,再次运行查询。

尝试在查询下方运行

select 
U.session_id,
U.session_date,
U.email
from data.usage U
left outer join
  (select 
  distinct M.session_id
  from data.usage M
  where email like '%gmail.com%'
  and data_date >= '20180101'
  and name in
    ( 
    select 
    lower(name)
    from data.users
    where role like 'Person%' 
    and isactive = TRUE
    and data_date = '20180412'
    ))M
on U.session_id = M.session_id
相关问题