Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

8
  • 95
    That's clever, and relatively simple given that there exists no Median() aggregate function. But how is it that no Median() function exists!? I'm a bit FLOOR()ed, frankly. Commented Jan 31, 2012 at 19:42
  • 1
    ... I mean like in this case (the 2nd query named "Users with highest median answer score").
    – Tomas
    Commented Jul 1, 2013 at 13:02
  • 5
    How to use this solution with a GROUP BY? Commented May 13, 2015 at 9:32
  • 2
    Is this correct for odd number of results? Like for 9 elements median is the fifth one. Commented Feb 6, 2018 at 1:46
  • 3
    If "Score" field is nullable, the median won't compute. You can specify in query: 'SELECT ( (SELECT MAX(Score) FROM (SELECT TOP 50 PERCENT Score FROM Posts Where Score is not null ORDER BY Score) AS BottomHalf) + (SELECT MIN(Score) FROM (SELECT TOP 50 PERCENT Score FROM Posts Where Score is not null ORDER BY Score DESC) AS TopHalf) ) / 2 AS Median' Commented Jul 4, 2019 at 14:47