hi, i have the sql query below for a report, i need to calculate the median Concentration for the itemtype. any body know how i can do that? the median is the middle Concentration number of the group if it has old number of rows, and if its even,
it is the middle 2 number divided by 2.
SELECT [itemType], SUM(CASE WHEN Concentration = '-999' THEN 0 ELSE 1 END) as [Tsample]
,SUM(CASE WHEN Concentration = '-1' THEN 1 ELSE 0 END) as [Tnd]
,SUM(CASE WHEN Concentration != '-1' AND Concentration != '-999' THEN 1 ELSE 0 END) as [Tnnd]
,MIN(CASE WHEN Concentration = '-1' OR Concentration = '-999' THEN NULL ELSE Concentration END) as [Min]
, ---- Median Concentration for itemtype
,MAX(CASE WHEN Concentration = '-1' OR Concentration = '-999' THEN NULL ELSE Concentration END) as [Max]
from dbo.vwSampleDetails as a
GROUP by [itemType]
this will calculate the median if i was working with the whole table, but i need it to calculate inside the group by query
Select ((
Select Top 1 Concentration
From (
Select Top 50 Percent Concentration
From vwSampleDetails
Where Concentration != '-1' and Concentration != '-999'
Order By Concentration
) As A
Order By Concentration DESC) +
(
Select Top 1 Concentration
From (
Select Top 50 Percent Concentration
From vwSampleDetails
Where Concentration != '-1' and Concentration != '-999'
Order By Concentration DESC
) As A
Order By Concentration Asc)) / 2