I need some help with a median calculation. I can calculate the median length of stay for a group of patients with the following query I picked up.
SELECT Avg([R1]) AS Median
FROM [
SELECT Max(Q1.R2) AS R1
FROM
(SELECT TOP 50 PERCENT LOS_min_amb_work.los AS R2
FROM LOS_min_amb_work
ORDER BY LOS_min_amb_work.los ASC) AS Q1
UNION
SELECT Min(Q2.R2) AS R1
FROM
(SELECT TOP 50 PERCENT LOS_min_amb_work.los AS R2
FROM los_min_amb_work
ORDER BY LOS_min_amb_work.los DESC) AS Q2
]. AS Q3;
I have now been asked to calculate a median length of stay and group them by diagnosis. The LOS_min_amb_work.los is a query as follows where the TSheetTitleDescription is the diagnosis field I need to group by.
PARAMETERS [Enter Beginning Date] DateTime, [Enter Ending Date] DateTime;
SELECT Detail.[ED#], Detail.HospitalArrivalTime, TimeValue([HospitalArrivalTime]) AS HATTime, Detail.DispositionTime, IIf(([DispositionTime]-[HATTime])<0,(([DispositionTime]-[HATTime])+1)*1440,([DispositionTime]-[HATTime])*1440) AS LOS, Detail.PhysicianExamTime, IIf([DispositionTime]-[PhysicianExamTime]<0,([DispositionTime]-[PhysicianExamTime]+1)*1440,([DispositionTime]-[PhysicianExamTime])*1440) AS MDToDT, [Enter Beginning Date] AS Beg, [Enter Ending Date] AS [End], DateValue([Detail]![HospitalArrivalTime]) AS HAD, IIf([DispositionTime]-[DispositionOrderTime]<0,([DispositionTime]-[DispositionOrderTime]+1)*1440,([DispositionTime]-[DispositionOrderTime])*1440) AS DOTToDT, Detail.EDBedTime, Detail.DispositionOrderTime, IIf([DispositionTime]-[PhysicianExamTime]<0,([DispositionTime]-[PhysicianExamTime]+1)*1440,([DispositionTime]-[PhysicianExamTime])*1440) AS MDoDOT, Detail.DispositionCode, Detail.Lastname, Detail.TSheetTitle
FROM Detail
WHERE (((Detail.HospitalArrivalTime) Between [Enter Beginning Date] And ([Enter Ending Date]+1)) AND ((Detail.DispositionCode)=1 Or (Detail.DispositionCode)=3 Or (Detail.DispositionCode)=15 Or (Detail.DispositionCode)=21 Or (Detail.DispositionCode)=22 Or (Detail.DispositionCode)=23 Or (Detail.DispositionCode)=31));
Any help in getting the median query to function with grouping would be greatly appreciated.
SELECT Avg([R1]) AS Median
FROM [
SELECT Max(Q1.R2) AS R1
FROM
(SELECT TOP 50 PERCENT LOS_min_amb_work.los AS R2
FROM LOS_min_amb_work
ORDER BY LOS_min_amb_work.los ASC) AS Q1
UNION
SELECT Min(Q2.R2) AS R1
FROM
(SELECT TOP 50 PERCENT LOS_min_amb_work.los AS R2
FROM los_min_amb_work
ORDER BY LOS_min_amb_work.los DESC) AS Q2
]. AS Q3;
I have now been asked to calculate a median length of stay and group them by diagnosis. The LOS_min_amb_work.los is a query as follows where the TSheetTitleDescription is the diagnosis field I need to group by.
PARAMETERS [Enter Beginning Date] DateTime, [Enter Ending Date] DateTime;
SELECT Detail.[ED#], Detail.HospitalArrivalTime, TimeValue([HospitalArrivalTime]) AS HATTime, Detail.DispositionTime, IIf(([DispositionTime]-[HATTime])<0,(([DispositionTime]-[HATTime])+1)*1440,([DispositionTime]-[HATTime])*1440) AS LOS, Detail.PhysicianExamTime, IIf([DispositionTime]-[PhysicianExamTime]<0,([DispositionTime]-[PhysicianExamTime]+1)*1440,([DispositionTime]-[PhysicianExamTime])*1440) AS MDToDT, [Enter Beginning Date] AS Beg, [Enter Ending Date] AS [End], DateValue([Detail]![HospitalArrivalTime]) AS HAD, IIf([DispositionTime]-[DispositionOrderTime]<0,([DispositionTime]-[DispositionOrderTime]+1)*1440,([DispositionTime]-[DispositionOrderTime])*1440) AS DOTToDT, Detail.EDBedTime, Detail.DispositionOrderTime, IIf([DispositionTime]-[PhysicianExamTime]<0,([DispositionTime]-[PhysicianExamTime]+1)*1440,([DispositionTime]-[PhysicianExamTime])*1440) AS MDoDOT, Detail.DispositionCode, Detail.Lastname, Detail.TSheetTitle
FROM Detail
WHERE (((Detail.HospitalArrivalTime) Between [Enter Beginning Date] And ([Enter Ending Date]+1)) AND ((Detail.DispositionCode)=1 Or (Detail.DispositionCode)=3 Or (Detail.DispositionCode)=15 Or (Detail.DispositionCode)=21 Or (Detail.DispositionCode)=22 Or (Detail.DispositionCode)=23 Or (Detail.DispositionCode)=31));
Any help in getting the median query to function with grouping would be greatly appreciated.