繁体   English   中英

多对多加入过滤器

[英]Many to many join with filter

我有两张这样的桌子 -

表格1 -

patient   admit_dt     discharge_dt
323       2020-01-09   2020-02-01
323       2020-02-18   2020-02-27
231       2020-02-13   2020-02-17

表 2 -

patient   admit_dt     discharge_dt
323       2020-02-05   2020-02-07
231       2020-02-23   2020-02-28

我需要的 output 是

patient   
323  

逻辑是 - 如果一名患者在 30 天内从table 1进入table 2并最终回到table 1 ,我们希望将他们计入 output。

结果中不包括Patient 231 ,因为他们没有 go 返回table 1

如果我理解正确,您可以使用join

select t1.patient
from table1 t1 join
     table2 t2
     on t2.patient = t1.patient and
        t2.admit_dt > t1.discharge_dt join
     table1 tt1
     on tt1.patient = t1.patient and
        tt1.admit_dt > t2.discharge_dt;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM