Skip to content

Instantly share code, notes, and snippets.

@rdmurphy
Created May 5, 2018 16:14
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdmurphy/3f73c7b1826cacee34f6c2a855b12e2e to your computer and use it in GitHub Desktop.
Save rdmurphy/3f73c7b1826cacee34f6c2a855b12e2e to your computer and use it in GitHub Desktop.
PostgreSQL specific Django Median aggregation function
from django.db.models import Aggregate, FloatField
class Median(Aggregate):
function = 'PERCENTILE_CONT'
name = 'median'
output_field = FloatField()
template = '%(function)s(0.5) WITHIN GROUP (ORDER BY %(expressions)s)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment