简体   繁体   English

按django分组,不同

[英]Group by, distinct, count in django

What would be the equivalent statement for querying model in Django 在Django中查询模型的等效语句是什么

select distinct final_category, count(responders) 
from johnson_jnjusage 
where no_of_people_house = "4" and 
      child_age_group="0 to 12 months" and 
      city = "HYDERABAD" and 
      nursing_cnt = "2ND TIME MOTHER" and 
      bucket="BRAND PENETRATION" 
group by final_category;

Thanks 谢谢

Based on the Django docs for aggregation , it might look something like this: 根据Django文档进行汇总 ,可能看起来像这样:

from django.db.models import Count
Usage.objects.filter(no_of_people_house='4', city='HYDERABAD', nursing_cnt='2ND TIME MOTHER', bucket='BRAND PENETRATION').values('final_category').annotate(responders=Count('responders'))

The order of the filter, values and annotate clauses is important as it defines how the aggregation behaves. 过滤器,值和注释子句的顺序很重要,因为它定义了聚合的行为方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM