通过聚合连接多个表以供查看

qvsjd97n  于 2021-06-26  发布在  Impala
关注(0)|答案(1)|浏览(255)

下面的查询返回每个src\u sys\u id对应的最大时间戳记录。
选择distinct s1.src\u sys\u id,s1.execn\u ts,s3.cd\u id,s4.appl\u nm,case when s1.st\u mgmnt\u cd!“purge“then‘in progress’else‘in compliance’end as status from dlrm\u data\u retention.ret\u st\u aud s1 inner join(选择src\u sys\u id,max(execn\u ts)as maxtime from dlrm\u data\u retention.ret\u st\u aud group by src\u sys\u id=s1.src\u sys\u id和s1.execn\u ts=s2.maxtime
如何将此查询与其他表连接起来,以获得与此src\u sys\u id相对应的更多列。
表3-这个有src\u sys\u id和cd\u id表4-这个有cd\u id和src\u name。
我需要将cd\u id和src\u name列也作为select语句的一部分。
我试过下面的。它不返回任何结果。
选择distinct s1.src\u sys\u id,s1.execn\u ts,case when s1.st\u mgmnt\u cd!=“清除“然后”“进行中”“否则”“符合”结束为状态,从dlrm\u data\u retention.ret\u st\u aud s1 join table3 s3 join table4 s4 on s1.src\u sys\u id=s3.src\u sys\u id and s3.cd\u id=s4.cd\u id
内部联接(从dlrm\u data\u retention.ret\u st\u aud group by src\u sys\u id=s1.src\u sys\u id和s1.execn\u ts=s2.maxtime上选择src\u sys\u id,max(execn\u ts)作为maxtime)

jk9hmnmh

jk9hmnmh1#

希望这有帮助。。

select distinct s1.src_sys_id,
    s1.execn_ts,
    CASE WHEN s1.st_mgmnt_cd != "PURGE" 
    THEN 'In-Progress' 
    ELSE 'In-Compliance' 
    END as Status

    from dlrm_data_retention.ret_st_aud As  s1 

    JOIN table3 As s3 ON s3.src_sys_id = s1.src_sys_id 

    JOIN table4 As s4 ON  s4.cd_id = s3.cd_id    

    inner join 
    (
      SELECT src_sys_id, max(execn_ts) as maxtime
      FROM dlrm_data_retention.ret_st_aud 
      GROUP BY src_sys_id 
     ) As s2 on s2.src_sys_id = s1.src_sys_id and s1.execn_ts = s2.maxtime

相关问题