Calculating Medians With SQL
Given that median is such a valuable statistical measure, it’s baffling that Microsoft’s SQL Server and other relational databases (MySQL, PostgreSQL) don’t have a built-in MEDIAN function. Well, this week, after working through a data set in SQL Server — and deciding I didn’t want to push the data into SPSS to find medians — I hit the web to find a T-SQL workaround.
I found a ton of solutions (some from people with no clue about the difference between median and average), but the one below — adapted from a post by Adam Machanic at sqlblog.com — was the best. It produces accurate results and is fairly speedy to boot.
Here’s an example. Consider this table with student grades from two courses:
.
| ID | Class | FirstName | Grade |
|---|---|---|---|
| 1 | Math | Bob | 65 |
| 2 | Math | Joe | 72 |
| 3 | Math | Sally | 95 |
| 4 | Science | Bob | 65 |
| 5 | Science | Joe | 81 |
| 6 | Science | Sally | 81 |
| 7 | Science | Mike | 72 |
We’d like to find the median grade in each class. Here’s the script:
(more…)
