I have this SQL for MS SQL Server 2008:
WITH CTE AS (
SELECT e_id,
scale,
ROW_NUMBER() OVER(PARTITION BY e_id ORDER BY scale ASC) AS rn,
COUNT(scale) OVER(PARTITION BY e_id) AS cn
FROM waypoint.dbo.ScoreMaster
WHERE scale IS NOT NULL
)
SELECT e_id,
cast(AVG (cast(scale as decimal(5,2))) as decimal(5,3)) as [AVG],
cast (STDEV(cast(scale as decimal(5,1))) as decimal(5,3)) as [STDDEV],
AVG(CASE WHEN 2 * rn - cn BETWEEN 0 AND 2 THEN
scale END) AS FinancialMedian,
MAX(CASE WHEN 2 * rn - cn BETWEEN 0 AND 2 THEN
scale END) AS StatisticalMedian
from CTE
GROUP BY e_id
and I would like to add the Mode. I've been trying different ideas, but nothing works yet...