Skip to content

Instantly share code, notes, and snippets.

@ketanbhatt
Last active July 8, 2017 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ketanbhatt/5522896c4e0fe2d9a5a9554743caf4be to your computer and use it in GitHub Desktop.
Save ketanbhatt/5522896c4e0fe2d9a5a9554743caf4be to your computer and use it in GitHub Desktop.
Original Query with RawSQL
q = MyModel.objects.values('some_fk_id').annotate(
avg_duration=Avg('duration'),
perc_90_duration=RawSQL('percentile_disc(%s) WITHIN GROUP (ORDER BY duration)', (0.9,)),
)
print q.query
# SELECT "some_fk_id",
# AVG("duration") AS "avg_duration",
# (percentile_disc(0.9) WITHIN
# GROUP (
# ORDER BY duration)) AS "perc_90_duration"
# FROM "mymodel"
# GROUP BY "some_fk_id", (percentile_disc(0.9) WITHIN
# GROUP (
# ORDER BY duration))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment