python - django 中的分组依据、不同值、计数

标签 python mysql django django-queryset

在 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;

谢谢

最佳答案

基于Django docs for aggregation ,它可能看起来像这样:

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'))

过滤器、值和注释子句的顺序很重要,因为它定义了聚合的行为方式。

关于python - django 中的分组依据、不同值、计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25160064/

相关文章:

mysql - 在django中采样mysql结果

python - django - 安排重复任务间隔

python - 我想通过使用 ansible 在属性文件中查找来替换定界符之间的字符串

python - 如何编写一个以列表和值作为参数并返回第一个出现值的索引的函数?

python - 无法导入守护程序类

python - 将 Pandas 列转换为字典,其中列标题为键,列值作为值

mysql - 从 MySQL 获取 MAC 地址

php - 如何自动同步mysql数据库和solr

python - logger.removeHandler(logger.handlers[0]) 如何抛出 ValueError : list. 删除(x) : x not in list?

Mysql在表上加入两次