How to make query with grouping by foreign keys with ordering by sum of field

artem

For example I have a model:

class Reward(models.Model):
   user = models.ForeignKey(UserProfile)
   amount = models.IntegerField()

I want to get such statistics:

[{ 
   "user": user1,
   "total_amount": 100500, // Sum of amount field of all rewards for this user
   "total_rewards": 13 // Count of rewards for this user
},
{
   "user": user2,
   ...
}
]

This list should be ordered by total_amount field. How should I do such query?

ilse2005

You can do that with annotate

from django.db.models import Count, Sum
UserProfile.objects.annotate(
    total_amount=Sum('reward__amount'),
    total_rewards=Count('reward')
).values('id', 'total_amount', 'total_rewards').order_by('total_amount')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Grouping and Sum some field with Sub query in LINQ

From Dev

Query sum other table field and grouping

From Dev

Trying to make a PostgreSQL field with a list of foreign keys in Django

From Dev

Access Query Grouping/Sum

From Dev

ordering query by grouped, calculated field

From Dev

SQLite field with a list of foreign keys

From Dev

How to make two foreign keys to same model unique together?

From Dev

How to make Ruby on Rails create SQLite database with foreign keys enabled?

From Dev

How to make foreign keys of a value be displayed in primefaces datatable

From Dev

How to make UserID and ProductID columns as the foreign keys in Downloads Table?

From Dev

How should I name and make foreign keys in Rails 5.1?

From Dev

mongodb How to make age field in sum not count

From Dev

How to query pivot table with multiple foreign keys Laravel 5 Eloquent

From Dev

How to efficiently write DISTINCT query in Django with table having foreign keys

From Dev

SQL Query CASE with SUM and Grouping

From Dev

How to make a query with optional keys of an object?

From Dev

Query a table with 2 foreign keys

From Dev

MySQL query for getting all foreign keys and the sum of all their columns in m:n table

From Dev

How to make a modelform editable foreign key field in a django template?

From Dev

How to make ARRAY field with foreign key constraint in SQLAlchemy?

From Dev

How to make a field which will be foreign key and primary key at the same time

From Dev

How to make a field which will be foreign key and primary key at the same time

From Java

How to make a sum only query in a whole collection?

From Dev

Cumulative sum query on foreign key

From Dev

how to perform a search query of a foreign key field's attribute in rails?

From Dev

How to make this query aggregate the time-grouping correct?

From Dev

How To Make Make Auto Sum Calculation Input Field

From Dev

How To Make Make Auto Sum Calculation Input Field

From Dev

How to make a query on related_name field?

Related Related

  1. 1

    Grouping and Sum some field with Sub query in LINQ

  2. 2

    Query sum other table field and grouping

  3. 3

    Trying to make a PostgreSQL field with a list of foreign keys in Django

  4. 4

    Access Query Grouping/Sum

  5. 5

    ordering query by grouped, calculated field

  6. 6

    SQLite field with a list of foreign keys

  7. 7

    How to make two foreign keys to same model unique together?

  8. 8

    How to make Ruby on Rails create SQLite database with foreign keys enabled?

  9. 9

    How to make foreign keys of a value be displayed in primefaces datatable

  10. 10

    How to make UserID and ProductID columns as the foreign keys in Downloads Table?

  11. 11

    How should I name and make foreign keys in Rails 5.1?

  12. 12

    mongodb How to make age field in sum not count

  13. 13

    How to query pivot table with multiple foreign keys Laravel 5 Eloquent

  14. 14

    How to efficiently write DISTINCT query in Django with table having foreign keys

  15. 15

    SQL Query CASE with SUM and Grouping

  16. 16

    How to make a query with optional keys of an object?

  17. 17

    Query a table with 2 foreign keys

  18. 18

    MySQL query for getting all foreign keys and the sum of all their columns in m:n table

  19. 19

    How to make a modelform editable foreign key field in a django template?

  20. 20

    How to make ARRAY field with foreign key constraint in SQLAlchemy?

  21. 21

    How to make a field which will be foreign key and primary key at the same time

  22. 22

    How to make a field which will be foreign key and primary key at the same time

  23. 23

    How to make a sum only query in a whole collection?

  24. 24

    Cumulative sum query on foreign key

  25. 25

    how to perform a search query of a foreign key field's attribute in rails?

  26. 26

    How to make this query aggregate the time-grouping correct?

  27. 27

    How To Make Make Auto Sum Calculation Input Field

  28. 28

    How To Make Make Auto Sum Calculation Input Field

  29. 29

    How to make a query on related_name field?

HotTag

Archive