I happened to need Median recently and came up with this solution.
SELECT TOP 1 AVG(main.age) AS _Average,min(main.age) AS _Min, CASE WHEN %vid = count(main.age)/2 THEN main.age else 0 END+MAX(CASE WHEN %vid = count(main.age)/2 THEN main.age else 0 END) AS _Median, max(main.age) AS _Max FROM ( SELECT TOP all a.Age FROM Sample.Person a ORDER BY a.Age ) main
And my 2 cents ..........
WHERE
datediff(year,
todate(to_char(dgi.Birthdate,'YYYY')||':1','YYYY:MM'),
todate(to_char(current_date,'YYYY')||':1','YYYY:MM')
) >= 13
Certifications & Credly badges:
Randy has no Certifications & Credly badges yet.
Global Masters badges:
Randy has no Global Masters badges yet.
Followers:
Randy has no followers yet.
Following:
Randy has not followed anybody yet.
SELECT TOP 1
AVG(main.age) AS _Average,min(main.age) AS _Min,
CASE WHEN %vid = count(main.age)/2 THEN main.age else 0 END+MAX(CASE WHEN %vid = count(main.age)/2 THEN main.age else 0 END) AS _Median,
max(main.age) AS _Max
FROM ( SELECT TOP all a.Age FROM Sample.Person a ORDER BY a.Age ) main