Randy Shaw · Apr 29, 2022 go to post

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

Randy Shaw · Aug 17, 2021 go to post

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
Randy Shaw · Dec 27, 2017 go to post

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

Randy Shaw · Oct 14, 2017 go to post

I am not sure there is a logical way for those tables to be joined. But for syntax purposes here is a query that works although, as mentioned,  illogically. 

SELECT
* FROM (SELECT * FROM Sample.Employee e LEFT JOIN Sample.Company c ON c.ID=e.Company) g
JOIN Sample.Vendor v ON g.Company=v.Vendor