View Single Post

   
  #1 (permalink)  
Old 04-29-2008, 08:26 PM
HumanJHawkins
 
Posts: n/a
Default How to show Median in a GROUP BY clause? (AVE works, Median doesn'

We are using SQL Server 2000. I have already learned that there is no
MEDIAN, or MODE function to go along with the AVE... I've also found
several methods of computing Median. The one I like best is:

-- Apologies for not citing the source... I lost the website and the
file I downloaded has no ID in it.
SELECT
((SELECT MAX(iNumericField) FROM
(SELECT TOP 50 PERCENT iNumericField FROM dbo.MyTable ORDER BY
iNumericField) AS W1) +
(SELECT MIN(iNumericField) FROM
(SELECT TOP 50 PERCENT iNumericField FROM dbo.MyTable ORDER BY
iNumericField DESC) AS W2)
) /2 AS Median;
-- END

However, now I want to use this in a GROUP BY clause and I have no
idea how.

-- What can I replace the non-functioning Median(flData) part with?
SELECT iBin, AVG(flData) AS Mean_Data, Median(flData) AS
Median_Data
FROM dbo.MyData
GROUP BY iBin
-- END

Is there some syntax for embedding the above "Get the Median" code
into my GROUP BY query? Is there some syntax to turn it into a
function that so I can make my own "Median()" function?

Many thanks in advance!
Reply With Quote