左联接不产生结果

ygya80vv  于 2021-06-26  发布在  Impala
关注(0)|答案(2)|浏览(355)

我有下面的查询,其中我需要从第一个表1和表2的相应值的所有记录。如果没有值有一个空返回。但我只得到结果中返回的普通记录。

select 

distinct s1.src_sys_id schema_nm,
to_date(CAST(CAST(s3.execn_ts AS BIGINT)/1000 AS TIMESTAMP)) as maxtime
from table1 s1

LEFT JOIN table2 s3 ON s1.src_sys_id = s3.src_sys_id

INNER JOIN 
(
  SELECT src_sys_id, max(execn_ts) as maxtime
  FROM table2
  GROUP BY src_sys_id 
) s2 on s2.src_sys_id = s1.src_sys_id and s1.execn_ts = s2.maxtime

表1

schema_nm   
GA1P    
GG60    
GGDD    
GWY1    
GXO1    
GYY1    
GHOD

表2

schema_nm   execn_ts
GA1P    10/1/2018
GG60    10/2/2018
GGDD    10/3/2018

当前结果

schema_nm   maxtime
GA1P    10/1/2018
GG60    10/2/2018
GGDD    10/3/2018

预期结果

schema_nm   maxtime
GA1P    10/1/2018
GG60    10/2/2018
GGDD    10/3/2018
GWY1    NULL
GXO1    NULL
GYY1    NULL
GHOD    NULL
6pp0gazn

6pp0gazn1#

我认为真正问题的答案是使用窗口函数:

select s1.src_sys_id schema_nm,
       to_date(CAST(CAST(s3.execn_ts AS BIGINT)/1000 AS TIMESTAMP)) as maxtime
from table1 s1 left join
     (select s2.*,
             row_number() over (partition by s2.src_sys_id order by s2.execn_ts desc) as seqnum
      from table2 s2
     ) s2
     on s1.src_sys_id = s2.src_sys_id and s2.seqnum = 0;

查询过滤掉行的原因有点棘手。这个 left join 很好。即使是 inner join 很好。到目前为止。但是,两个联接都指向同一个表。因此,如果第一个匹配行不存在 join ,则第二行不存在。
换言之 inner join 恰好筛选出与中不匹配的行相同的行 left join .
总之,窗口函数是一种更好的方法,尽管使用 left join 同样也能解决你的问题。

xwmevbvl

xwmevbvl2#

你的第一个 LEFT JOIN 是多余的:

select s1.src_sys_id schema_nm,
       to_date(CAST(CAST(s2.execn_ts AS BIGINT)/1000 AS TIMESTAMP)) as maxtime
from table1 s1 left join (
     select s2.src_sys_id, max(s2.execn_ts) as execn_ts
     from table2 s2
     group by s2.src_sys_id
) s2 on s1.src_sys_id = s2.src_sys_id; 
        --s2.execn_ts = s1.execn_ts;

相关问题