Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: GROUP BY two values

Tags:

I would basically like to do the same as this question, but grouping by combinations of two values, rather than just one:

SELECT player_type, team, COUNT(*) FROM players GROUP BY player_type, team; 

Does anyone know whether, and how, this is possible in Django? I'm using 1.2.

like image 360
AP257 Avatar asked May 26 '10 20:05

AP257


People also ask

How to use aggregate in Django?

When specifying the field to be aggregated in an aggregate function, Django will allow you to use the same double underscore notation that is used when referring to related fields in filters. Django will then handle any table joins that are required to retrieve and aggregate the related value.

What is annotate Django?

Appending the annotate() clause onto a QuerySet lets you add an attribute to each item in the QuerySet, like if you wanted to count the amount of articles in each category. However, sometimes you only want to count objects that match a certain condition, for example only counting articles that are published.


1 Answers

Player.objects.values('player_type', 'team').order_by().annotate(Count('player_type'), Count('team')) 
like image 129
Amarghosh Avatar answered Oct 18 '22 06:10

Amarghosh