简体   繁体   English

多对多加入过滤器

[英]Many to many join with filter

I have two tables like so -我有两张这样的桌子 -

Table 1 -表格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

Table 2 -表 2 -

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

The output I am needing is我需要的 output 是

patient   
323  

The logic is - if one patient goes from table 1 into table 2 and ends up back in table 1 within 30 days, we want to count them in the output.逻辑是 - 如果一名患者在 30 天内从table 1进入table 2并最终回到table 1 ,我们希望将他们计入 output。

Patient 231 is not included in the result because they didn't go back to table 1 .结果中不包括Patient 231 ,因为他们没有 go 返回table 1

If I understand correctly, you can use join :如果我理解正确,您可以使用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