Django查询来查找具有特定值(大于零)的行数(按用户分组) [英] 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.
我想为每个用户查找其得分大于零的天数-因此,我想对用户进行 group by
和 count
每个用户的分数均大于零.
I want to find for each user, the number of days on which he/she had a greater than zero score - so, I want to group by
user and count
number of scores greater than zero for each user.
如何在Django 1.10中编写这样的查询?
How do I write such a query in Django 1.10?
推荐答案
对于相当于得分大于(gt)零的所有对象,以下等效于按用户分组并计数条目数的SELECT查询:
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?
这篇关于Django查询来查找具有特定值(大于零)的行数(按用户分组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!