Django ORM to remove unwanted groups by annotating multiple aggregated columns


Facebook Facebook logo Sign up for Facebook to connect with Taranjeet Singh

I want to create a query like this in Django ORM.

SELECT COUNT(CASE WHEN myCondition THEN 1 ELSE NULL end) as numyear
FROM myTable

Below is the djang ORM query I wrote

year_case = Case(When(added_on__year = today.year, then=1), output_field=IntegerField())

qs = (ProfaneContent.objects
                    .annotate(numyear=Count(year_case))
                    .values('numyear'))

This is the query generated by the django orm.

SELECT COUNT(CASE WHEN "analyzer_profanecontent"."added_on" BETWEEN 2020-01-01 00:00:00+00:00 AND 2020-12-31 23:59:59.999999+00:00 THEN 1 ELSE NULL END) AS "numyear" FROM "analyzer_profanecontent" GROUP BY "analyzer_profanecontent"."id"

Everything else was fine, but django put a GROUP BY at the end , resulting in multiple lines and wrong answers. I don't want it at all. There's only one column right now, but I'll put more columns like this.

Edit based on comments I'm going to use the qs variable to get values ​​about how to sort in the current year, month, week.

UPDATE Based on the comments and answers I'm going to post here, let me clarify. I just want to do this on the database side (obviously using Django ORM instead of RAW SQL). It's a simple SQL query. Since the data can be too large, doing anything at the end of Python would be inefficient. That's why I want the database to give me the sum of the records based on the CASE condition. I'll add more such columns in the future, so things like len() or .count won't work.

I just want to create the above query using Django ORM (without GROUP BY appended automatically).

dasosula

When using aggregates in annotations, django needs some sort of grouping, if not, it defaults to the primary key. So you need to use .values()before .annotate(). See the django docs .

But to remove the group by completely you can use a static value, and django is smart enough to remove it completely, so you can get the result with an ORM query like this:

year_case = Case(When(added_on__year = today.year, then=1), output_field=IntegerField())

qs = (ProfaneContent.objects
                    .annotate(dummy_group_by = Value(1))
                    .values('dummy_group_by')
                    .annotate(numyear=Count(year_case))
                    .values('numyear'))

Related


Django ORM - aggregated by Decorator

Christopher I'm trying to use the Django ORM to aggregate by fields and sum the values returned by a decorator. I'm new to Django ORM, so please forgive me if I'm missing something. Here is my class: class Payroll(models.Model): job = models.ForeignKey(Job

Django ORM - aggregated by Decorator

Christopher I'm trying to use the Django ORM to aggregate by fields and sum the values returned by a decorator. I'm new to Django ORM, so please forgive me if I'm missing something. Here is my class: class Payroll(models.Model): job = models.ForeignKey(Job

Django ORM - aggregated by Decorator

Christopher I'm trying to use the Django ORM to aggregate by fields and sum the values returned by a decorator. I'm new to Django ORM, so please forgive me if I'm missing something. Here is my class: class Payroll(models.Model): job = models.ForeignKey(Job

Django ORM - aggregated by Decorator

Christopher I'm trying to use the Django ORM to aggregate by fields and sum the values returned by a decorator. I'm new to Django ORM, so please forgive me if I'm missing something. Here is my class: class Payroll(models.Model): job = models.ForeignKey(Job

Django ORM - aggregated by Decorator

Christopher I'm trying to use the Django ORM to aggregate by fields and sum the values returned by a decorator. I'm new to Django ORM, so please forgive me if I'm missing something. Here is my class: class Payroll(models.Model): job = models.ForeignKey(Job

Django ORM, sum of multiple columns

wowbrowser search I have a question, how can we filter by SUM of multiple columns. example: class Foo(models.Model): i1 = models.IntegerField() i2 = models.IntegerField() i3 = models.IntegerField() And I need to filter objects whose sum of i1, i2,

Django ORM, sum of multiple columns

wowbrowser search I have a question, how can we filter by SUM of multiple columns. example: class Foo(models.Model): i1 = models.IntegerField() i2 = models.IntegerField() i3 = models.IntegerField() And I need to filter objects whose sum of i1, i2,

Django ORM, sum of multiple columns

wowbrowser search I have a question, how can we filter by SUM of multiple columns. example: class Foo(models.Model): i1 = models.IntegerField() i2 = models.IntegerField() i3 = models.IntegerField() And I need to filter objects whose sum of i1, i2,

Django ORM group by multiple columns

Shiva Ramakrishna How to perform multi-column grouping in Django? I've only seen examples on one column of group by. Below is the query I am trying to convert to Django ORM. SELECT order_id,city,locality,login_time,sum(morning_hours),sum(afternoon_hours),sum(e

Django ORM group by multiple columns

Shiva Rama Krishna How to perform multi-column grouping in Django? I've only seen examples on one column of group by. Below is the query I am trying to convert to Django ORM. SELECT order_id,city,locality,login_time,sum(morning_hours),sum(afternoon_hours),sum(

Django ORM, sum of multiple columns

wowbrowser search I have a question, how can we filter by SUM of multiple columns. example: class Foo(models.Model): i1 = models.IntegerField() i2 = models.IntegerField() i3 = models.IntegerField() And I need to filter objects whose sum of i1, i2,

Django ORM, sum of multiple columns

wowbrowser search I have a question, how can we filter by SUM of multiple columns. example: class Foo(models.Model): i1 = models.IntegerField() i2 = models.IntegerField() i3 = models.IntegerField() And I need to filter objects whose sum of i1, i2,

Django ORM, sum of multiple columns

wowbrowser search I have a question, how can we filter by SUM of multiple columns. example: class Foo(models.Model): i1 = models.IntegerField() i2 = models.IntegerField() i3 = models.IntegerField() And I need to filter objects whose sum of i1, i2,

Django ORM group by multiple columns

Shiva Rama Krishna How to perform multi-column grouping in Django? I've only seen examples on one column of group by. Below is the query I am trying to convert to Django ORM. SELECT order_id,city,locality,login_time,sum(morning_hours),sum(afternoon_hours),sum(

remove unwanted columns in python dataframe

pradeept I am new to python/panda. This data frame is part of a lab exercise. Below is the data frame. The result has 20 rows and 384 columns. country almond angelica anise anise_seed apple apple_brandy / 55620 Switzerland No No No

remove unwanted columns in python dataframe

pradeept I am new to python/panda. This data frame is part of a lab exercise. Below is the data frame. The result has 20 rows and 384 columns. country almond angelica anise anise_seed apple apple_brandy / 55620 Switzerland No No No

remove unwanted columns in python dataframe

pradeept I am new to python/panda. This data frame is part of a lab exercise. Below is the data frame. The result has 20 rows and 384 columns. country almond angelica anise anise_seed apple apple_brandy / 55620 Switzerland No No No