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]
|