I was trying to implement a median from this solution (among others, but this seemed the simplest Median code): Function to Calculate Median in Sql Server
However, I'm having difficulty in its application. This is my current SQL query. My goal is to find the Median TotalTimeOnCall
for CallerComplaintTypeID
on a given Week, Month, and Department. I think my biggest issue is that I'm just fundamentally not understanding how to apply this Median function to achieve my results.
For example, if I needed an Average, instead, I could just change that ORDER BY
to a GROUP BY
and then slap an AVG(TotalTimeOnCall)
instead. How do I accomplish this idea with this Median solution, instead?
This is the "raw data" query:
WITH rawData as (
SELECT
DepartmentName
,MONTH(PlacedOnLocal) AS MonthNumber
,CASE
WHEN Datepart(day, PlacedOnLocal) < 8 THEN '1'
WHEN Datepart(day, PlacedOnLocal) < 15 THEN '2'
WHEN Datepart(day, PlacedOnLocal) < 22 THEN '3'
WHEN Datepart(day, PlacedOnLocal) < 29 THEN '4'
ELSE '5'
END AS WeekNumber
,CallerComplaintTypeID
,TotalTimeOnCall
FROM [THE_RELEVANT_TABLE]
WHERE PlacedOnLocal BETWEEN '2014-09-01' AND '2014-12-31'
AND CallerComplaintTypeID IN (5,89,9,31,203)
AND TotalTimeOnCall IS NOT NULL
)
SELECT
DepartmentName,
MonthNumber,
WeekNumber,
CallerComplaintTypeID,
TotalTimeOnCall
FROM
rawData
ORDER BY DepartmentName, MonthNumber, WeekNumber, CallerComplaintTypeID
with this sample output:
DepartmentName MonthNumber WeekNumber CallerComplaintTypeID TotalTimeOnCall
Dept_01 9 1 5 654
Dept_01 9 1 5 156
Dept_01 9 1 5 21
Dept_01 9 1 5 67
Dept_01 9 1 5 13
Dept_01 9 1 5 97
Dept_01 9 1 5 87
Dept_01 9 1 5 16
this is the Median solution from above:
SELECT
(
(
SELECT MAX(TotalTimeOnCall)
FROM
(
SELECT TOP 50 PERCENT TotalTimeOnCall
FROM rawData
WHERE TotalTimeOnCall IS NOT NULL
ORDER BY TotalTimeOnCall
) AS BottomHalf
)
+
(
SELECT MIN(TotalTimeOnCall)
FROM
(
SELECT TOP 50 PERCENT TotalTimeOnCall
FROM rawData
WHERE TotalTimeOnCall IS NOT NULL
ORDER BY TotalTimeOnCall DESC
) AS TopHalf
)
) / 2 AS Median