根据SQL Server中的遭遇计算重新读取次数

时间:2017-05-04 16:54:28

标签: sql sql-server

***抱歉,我只是根据查询提供了正确的数据。

我正在根据录取日期寻找再入院资格。任何具有允许日期的ptID,在前一个允许日期的30天内按遭遇分组。每次遭遇需要最低允许日期。我们可以根据下面的数据说明四月份。像这样的许多问题,但他们没有多个遭遇ID。我也知道日期作为参数存在问题。

我的表格中有以下数据;

    encounter       PtID        admit               discharge
    ------------------------------------------------------------------
    V00018357259    M00104410   4/12/17 8:36 AM     NULL
    V00018357259    M00104410   4/12/17 8:36 AM     4/12/17 1:55 PM
    V00018362040    M00104410   4/13/17 4:38 PM     NULL
    V00018362040    M00104410   4/13/17 5:49 PM     NULL
    V00018362040    M00104410   4/13/17 11:04 PM    4/13/17 6:01 PM
    V00018362040    M00104410   4/13/17 11:04 PM    4/14/17 11:30 AM
    V00018408554    M00104410   5/3/17 9:51 AM      NULL
    V00018406101    M00104410   5/3/17 10:12 AM     NULL

希望收到以下内容作为输出:

    EncounterID   PtID         Admit             Readmission       DaySpan
    -----------------------------------------------------------------------
    V00018357259  M00104410    4/12/17 8:36 AM   4/13/17 4:38 PM     1 
    V00018357259  M00104410    4/12/17 8:36 AM   5/3/17 9:51 AM      21 
    V00018357259  M00104410    4/12/17 8:36 AM   5/3/17 10:12 AM     21
    V00018362040  M00104410    4/13/17 4:38 PM   5/3/17 9:51 AM      20
    V00018362040  M00104410    4/13/17 4:38 PM   5/3/17 10:12 AM     20
    V00018408554  M00104410    5/3/17 9:51 AM    5/3/17 10:12 AM     0 

我有当前的查询

select  
    av.patient_id, 
    av.encounter, 
    av.admit_date, 
    av1.admit_date as readmission,
    datediff(d, av.admit_date, av1.admit_date) as dayspan
from 
    adt_visit av
left join 
    adt_visit av1 on av1.patient_id = av.patient_id
where 
    (datediff(d, av.admit_date, av1.admit_date) > 0)
    and (datediff(d, av.admit_date, av1.admit_date) <= 30)
    and av.admit_date >'4/1/2017' and av.admit_date < '4/30/2017'
    and av.patient_id = 'M00104410'  
group by
     av.patient_id
     ,av.encounter
     , av.admit_date
     , av1.admit_date

产生这个:

     PtID       Encounter       Admit               Readmission       Dayspan
     ------------------------------------------------------------------------
     M00104410  V00018357259    4/12/17 8:36 AM     4/13/17 4:38 PM     1
     M00104410  V00018357259    4/12/17 8:36 AM     4/13/17 5:49 PM     1
     M00104410  V00018357259    4/12/17 8:36 AM     4/13/17 11:04 PM    1
     M00104410  V00018357259    4/12/17 8:36 AM     5/3/17 9:51 AM      21
     M00104410  V00018357259    4/12/17 8:36 AM     5/3/17 10:12 AM     21
     M00104410  V00018362040    4/13/17 4:38 PM     5/3/17 9:51 AM      20
     M00104410  V00018362040    4/13/17 4:38 PM     5/3/17 10:12 AM     20

有人可以帮忙吗?谢谢!

0 个答案:

没有答案