Skip to content
Advertisement

Include Medians for grouped query .

I have this query that I need to graph.
The graph will show Total patients, patient Longest wait (minutes) and patient Median wait for each location. I need to include Medians in my query, and that is where I am stuck.

Example Raw Data:

enter image description here

Query I have so far w/o Medians:

Output:

enter image description here

I need help getting a last column of Medians for WaitTime (from the raw data) for each location. Any help would be appreciated; Thanks!!

Desired Output:

enter image description here

Advertisement

Answer

There are PERCENTILE_CONT(for continues values), PERCENTILE_DISC(for discrete value) methods in Sqlserver 2012 for doing that. you can read more about it here.

Update: based on performance issues in this method that Aaron commented, I add a pure SQL calculation for Median:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement