Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
Django nested aggregates with group_by

I have a query

   select avg(total) from
       (select date, sum(value) as total from table group by some_field, date) as res
   group by week(date)

This is the way I'm getting sum of metrics for each date and show average result grouped by week. How could I get the same result with Django ORM not using raw query.

So far I tried the raw() with this query. Also I can get the results for inner select as

model_obj.objects.filter(some_field='some_value')\
               .values('date', 'some_field')\
               .order_by('date', 'some_field')\
               .annotate(total=Sum('value'))\
               .all()
about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!