计算SQL中的重新调用

时间:2013-09-06 21:18:38

标签: sql

我正在尝试计算我的数据集中的患者再入院率,但必须使用SQL来计算。我遇到了麻烦,希望有人能指出我正确的方向。我的数据显示如此

  

>   ID FirstDateofService LastDateofService PerformingProvider等(更多不相关的行)
  2202 2012-09-18 2012-09-21圣卢克的   2202 2012-09-29 2012-10-04 St. Luke's   4280 2012-09-19 2012-09-20 Seton
  4280 2012-09-19 2012-10-18 Seton
  4280 2012-09-21 2012-10-09 Seton
  4280 2012-10-10 2012-10-17塞顿
  8980 2013-06-20 2013-06-24丹顿地区   8980 2013-07-06 2013-07-06丹顿地区

数据一直在继续。我正在尝试编写一个查询来计算重新入院的次数(想要按照身份证明进行),其中重新入院是在30天或更短时间内从同一家医院获释后入院的。我尝试了很多方法,但无法得出准确的结果。任何建议,将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:1)

这假设人们不能在他们离开的同一天重新入学。它还假设Id是某种患者标识符。

Select
    Id,
    Count(*) as Readmissions
From
    table a2 -- a2 happens after a1
Where
    Exists (
        Select 'x'
    From
        table a1 
    Where
        a1.Id = a2.Id And
        a1.PerformingProvider = a2.PerformingProvider And
        a1.LastDateOfService < a2.FirstDateOfService And
        dateadd(day, 30, a1.LastDateOfService) >= a2.FirstDateOfService
    )
Group By
    Id

答案 1 :(得分:0)

/*

drop table #t
create table #t (id int, FirstDateOfService datetime, LastDateOfService datetime,         PerformingProvider varchar(30));
go
insert into #t (id, FirstDateOfService, LastDateOfService, PerformingProvider)
values
(2202  , '2012-09-18' , '2012-09-21' ,           'St. Luke''s')
,(2202   , '2012-09-29' , '2012-10-04',            'St. Luke''s')
,(4280   , '2012-09-19' , '2012-09-20',            'Seton')
,(4280   , '2012-09-19' , '2012-10-18',            'Seton')
,(4280   , '2012-09-21' , '2012-10-09',            'Seton')
,(4280   , '2012-10-10' , '2012-10-17',            'Seton')
,(8980   , '2013-06-20' , '2013-06-24',            'Denton Regional')
,(8980   , '2013-07-06' , '2013-07-06' ,           'Denton Regional')
*/
;with cte (rid, id, FirstDateOfService, LastDateOfService, PerformingProvider) 
as (
    select rid=row_number() over (PARTITION BY id order by FirstDateOfService),*
    from #t
)
select t1.id, t1.PerformingProvider, Readmission=count(*)
from cte t1
inner join cte t2
on t1.id = t2.id
and t1.rid = t2.rid-1
and t2.FirstDateOfService < dateadd(day, 30, t1.LastDateOfService)
and t1.PerformingProvider = t2.PerformingProvider
group by t1.id, t1.PerformingProvider
order by t1.id;

但我认为您的数据需要清理。为什么id = 4280有两个相同的firstdayofservice(2012-09-19)但是不同的lastdayofservice?