Hello All,
I am working on a research request to find out how many days a patient was in ICU during the course of his visit. Below is the sample data.
If you see the patient_id 1 he came into a Non ICU on 2013-02-05(which is service_start) and was tranferred to ICU on 2013-02-05(Transfer) and again from ICU to Non ICU on 2013-02-06 (Transfer) and now from Non ICU he is discharged on 2013-02-08(which is transfer time=service_end) making his total days in ICU as 2(2/5 and 2/6).Everthing runs fine with my query but if you see the patient_id 2 he was admitted in Non ICU on 3/10 and is moved to ICU on 3/11 and from ICU he was discharged on 3/15(transfer=service_end) from ICU. Making his total days in ICU as
5 days (11,12,13,14,15) but my query is giving my different result, i think because i am using service_start date but to find this i think i should use only transfer date not sure.
Could anyone please help me out to build up a solution for this.
Here is the query for the same sample data i have and the query i am using.
declare @Tbl table (patent_id int, visit_id int, [name of PR] nvarchar(100),Transfer datetime, service_start datetime,service_end datetime) insert into @Tbl values(1,100,'Not ICU','02/05/2013','02/05/2013','02/08/2013') insert into @Tbl values(1,100,'ICU','02/06/2013','02/05/2013','02/08/2013') insert into @Tbl values(1,100,'Not ICU','02/08/2013','02/05/2013','02/08/2013') insert into @Tbl values(2,200,'Not ICU','03/11/2013','03/10/2013','03/15/2013') insert into @Tbl values(2,200,'ICU','03/15/2013','03/10/2013','03/15/2013') insert into @Tbl values(3,400,'ICU','10/15/2013','10/15/2013','10/20/2013') insert into @Tbl values(3,400,'Not ICU','10/18/2013','10/15/2013','10/20/2013') insert into @Tbl values(3,400,'ICU','10/20/2013','10/15/2013','10/20/2013') ------------------------ select DISTINCT * into #temp from @Tbl ORDER BY Transfer --My Query select patent_id,SUM(days) from( select *,DATEDIFF(dd,case when service_start,transfer)+1 days from #temp )x group by patent_id
Note: Transfer is the date when he is/will be transferred to the ICU/NOT ICU/Discharged
Thanks
SQL Killer