Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

371
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda