1

I have been trying to implement pretty much the same thing as in query-for-total-score

Does Django support multilevel aggregation? I can get the max score for each user each challenge, but I don't know how to drill down my results to fit in.

s = Submission.objects.values('challenge','user').annotate(Max('score'),Max('total_score'))

This gives me list of dict with user, challenge, total_score and score

What I want is sum of scores for each user. When I use aggregate on this I am getting the sum of all scores

s.aggregate(Sum('total_score__max'),Sum(score__max))

Postgres sql query looks something like this

select "user_id", SUM("score__max") as "score", SUM("total_score__max") as "total_score", COUNT("challenge_id") as "count" from 
(SELECT "submission"."user_id", "submission"."challenge_id", MAX("submission"."score")
AS "score__max", MAX("submission"."total_score") AS "total_score__max" FROM "submission" 
GROUP BY "submission"."user_id", "submission"."challenge_id")t GROUP BY "user_id" ORDER BY "user_id";
Bhavani Ravi
  • 2,130
  • 3
  • 18
  • 41

0 Answers0