extrobe
3/10/2017 - 9:51 AM

Calculate the Median Value

Calculate the Median Value

-- Median
SELECT
(
 (SELECT MAX([Total Debt]) FROM
   (SELECT TOP 50 PERCENT [SALES] FROM dbo.Data ac	WHERE FIleDate = '20170306' ORDER BY [ac].[Sales]  ) AS BottomHalf)
 +
 (SELECT MIN([Total Debt]) FROM
   (SELECT TOP 50 PERCENT [SALES] FROM dbo.Data ac WHERE FIleDate = '20170306' ORDER BY [Sales] DESC) AS TopHalf)
) / 2 AS Median