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

378
Views
django - how to sum values of the same key?

I want to sum the value of the count key if the values of year_taken have duplicates, how can I do that?

enter image description here

so the output would look like this

{'year_taken': '2019-2020', 'count': 1}, {'year_taken': '2021-2022', 'count': 3}

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can try this:

from django.db.models import IntegerField, OuterRef, Subquery

class SQCount(Subquery):
    output_field = IntegerField()
    template = f"(SELECT count(*) from (%(subquery)s)) _count)"

subquery_count = SQCount(Subject.objects.filter(year_taken=OuterRef("year_taken")))

queryset = Subject.objects.annotate(count=subquery_count).order_by("year_taken").distinct("year_taken").values("year_taken", "count")
over 4 years ago · Santiago Trujillo Report
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!