View Single Post
  #3 (permalink)  
Old 04-19-2006, 05:56 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 92
ben has disabled reputation
Default ANY answer. Re: Calculate the median value of a column?

The following solution is credited to David Rozenshtein, Anatoly Abramovich, and Eugene Birger. More information can be found at http://www.oreilly.com/catalog/transqlcook/chapter/ch08.html.

To calculate the median of the light-bulb test results, use the following query:
[sql]
SELECT x.Hours median
FROM BulbLife x, BulbLife y
GROUP BY x.Hours
HAVING
SUM(CASE WHEN y.Hours [= x.Hours
THEN 1 ELSE 0 END)]=(COUNT(*)+1)/2 AND
SUM(CASE WHEN y.Hours >= x.Hours
THEN 1 ELSE 0 END)>=(COUNT(*)/2)+1
[/sql]
Reply With Quote