The Development version of Django has aggregate functions like Avg, Count, Max, Min, StdDev, Sum, and Variance (link text). Is there a reason Median is missing from the list?
Implementing one seems like it would be easy. Am I missing something? How much are the aggregate functions doing behind the scenes?
Here's your missing function. Pass it a queryset and the name of the column that you want to find the median for:
That wasn't as hard as some of the other responses seem to indicate. The important thing is to let the db sorting do all of the work, so if you have the column already indexed, this is a super cheap operation.
(update 1/28/2016) If you want to be more strict about the definition of median for an even number of items, this will average together the value of the two middle values.
A strong possibility is that median is not part of standard SQL.
Also, it requires a sort, making it quite expensive to compute.
I have no idea what db backend you are using, but if your db supports another aggregate, or you can find a clever way of doing it, You can probably access it easily by Aggregate.
FWIW, you can extend PostgreSQL 8.4 and above to have a median aggregate function with these code snippets.
Other code snippets (which work for older versions of PostgreSQL) are shown here. Be sure to read the comments for this resource.
Well, the reason is probably that you need to track all the numbers to calculate median. Avg, Count, Max, Min, StDev, Sum, and Variance can all be calculated with constant storage needs. That is, once you "record" a number you'll never need it again.
FWIW, the variables you need to track are: min, max, count,
<n>
= avg,<n^2>
= avg of the square of the values.Because median isn't a SQL aggregate. See, for example, the list of PostgreSQL aggregate functions and the list of MySQL aggregate functions.