Django Query To Find Number Of Rows With A Certain Value Greater Than Zero, Grouped By User
I have a dataset such that each user has an integer as a score for each date in a certain date range. I want to find for each user, the number of days on which he/she had a greater
Solution 1:
The below is equivalent to a SELECT query grouping by user and counting the number of entries, for all objects where the score is greater than (gt) zero:
from django.db.models import Count
results = MyObject.objects.filter(score__gt=0).values('user').annotate(total=Count('user')).order_by('user')
Which unless I'm mistaken is what you're asking for?
Post a Comment for "Django Query To Find Number Of Rows With A Certain Value Greater Than Zero, Grouped By User"