指示日期是否为 < 6 months before another date for the same ID column in SQL

问题描述 投票:0回答:1

我有这样的东西:

Id     Visit_Date        Visit_code      
A30     15May2004         Surgery            
A30     05FEB2005         Office             
B01     07DEC2002         Office             
B01     21NOV2002         Surgery           

我需要这个,其中有一栏表明此人是否在就诊后 6 个月内接受过手术

ID     Office_Visit    SX_past_6mo
A30        1              0
B01        1              1

我正在尝试这样的事情,但子查询遇到问题

proc sql;
select case when 
case when
visit_type="Office" then 1 else 0 end as office_visit,
case when 
visit_type="Office" and
DATEDIFF(MM, visit_date, select(visit_date where visit_type="Surgery") > 6 then 1 else 0 end as sx_past_6mo
group by Id
sql date filter sas where-clause
1个回答
0
投票

看起来这可能是一个家庭作业问题,所以我不会给出完整的答案,但我会让你开始。我认为您需要将表格连接到自身。

select pas.Id as ID, pas.Visit_Date as SurgeryDate, pao.Visit_Date as OfficeDate  from PatientActivity pas left join PatientActivity pao on pas.Id = pao.Id and pao.Visit_code = 'Office' where pas.Visit_code = 'Surgery' 

© www.soinside.com 2019 - 2024. All rights reserved.