代码:

from django.db.models import Sum
alarm_sum_group_items = models.FILE_PROTECT_ALARM.objects.filter(customer_id=customer_id).values('device_hash').annotate(alarm_sum=Sum('cmn_merge_count'))
data = []
for item in alarm_sum_group_items:
tmp = {}
tmp['customer_id'] = customer_id
tmp['device_hash'] = item['device_hash']
tmp['alarm_sum'] = item['alarm_sum']
data.append(tmp)
return APIResponse(status=status_code.success, data=data)

参考资料:

https://www.douban.com/group/topic/18930354/?type=like

http://www.tuicool.com/articles/aiUBVrn

https://www.douban.com/group/topic/18930354/?type=like

http://stackoverflow.com/questions/19101665/django-how-to-do-select-count-group-by-and-order-by

https://my.oschina.net/duoduo3369/blog/199190

http://stackoverflow.com/questions/327807/django-equivalent-for-count-and-group-by

https://segmentfault.com/q/1010000005828240

http://www.cnblogs.com/BeginMan/archive/2013/05/27/3101322.html

http://blog.csdn.net/iloveyin/article/details/46380619

http://www.jb51.net/article/69774.htm

【Django】Django 如何支持 分组查询、统计?的更多相关文章

  1. $Django 聚合函数、分组查询、F,Q查询、orm字段以及参数

        一.聚合函数    from django.db.models import Avg,Sum,Max,Min,Count,F,Q   #导入    # .查询图书的总价,平均价,最大价,最小价 ...

  2. Django的mode的分组查询和聚合查询和F查询和Q查询

    1.聚合查询 # 聚合函数aggregate,求指定字段的最大值,最小值,平均值,和的值,方法如下 from django.db.models import Avg from django.db.mo ...

  3. rails小技巧之分组查询统计并去重

    分组查询并统计 SpecialGroup.group(:special_type).count select special_type,count(*) from special_groups gro ...

  4. 借助case,实现更丰富的分组查询统计

    根据fileD6的前4位分组    分别统计该组  5种企业类型fileD31的数量 create or replace view jyjc_bycity as select substr(fileD ...

  5. Linq分组查询统计

    这里介绍Linq使用Group By和Count得到每个CategoryID中产品的数量,Linq使用Group By和Count得到每个CategoryID中断货产品的数量等方面. 学经常会遇到Li ...

  6. MySQL分组查询统计

    GROUP BY 1.有个店铺表,字段为区域id,设备数量,店铺名称,如下: 2.如何按区域 district 统计 每个区域的设备数量,SQL如下 SELECT district, SUM( dev ...

  7. oracle-按年、月、周、日、时、分 分组查询统计数据,无数据补零(connect by)

    目的:统计一段时间内每年.每月.每周.每日.每时.每分数据,无数据时自动补零 思路:1. 生成给定时间段对应日期 2. 将原表中该时间段内的不为0的数据量统计出来 3. 用left join连接起来, ...

  8. linq 多表分组查询统计

    var q1 = from orderitem in q2 join pd in _iProductDetailContract.Entities on orderitem.ProductDetail ...

  9. 八、django学习之分组查询、F查询和Q查询

    分组查询.F查询和Q查询 分组查询 统计每个出版社出版的书籍的平均价格 第一种方式 obj = models.Book.objects.values('publishs_id').annotate(a ...

随机推荐

  1. 每天一个linux命令(39):iostat命令

    Linux系统中的 iostat 是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视.它的特点是汇报磁盘活动统计情况,同时也会 汇报出CPU使用情况 ...

  2. Java算法-各种题目总结

    1.排列计算 /*[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子 ...

  3. note.js之 Mongodb在Nodejs上的配置及session会话机制的实现

    上篇我们使用nodejs实现了一个express4的网站构建配置,但一个有面的网站怎么可以缺少一个数据库呢.现在较为流行的就是使用MONGODB来作为nodejs网站引用的数据库,可能它与nodejs ...

  4. 【HDU 2546】饭卡(DP+贪心)

    贪心:最贵的留到最后买.状态转移方程:dp[j]=dp[j+a[i]]|dp[j],dp[i]表示余下i元. 原来就不足5元,那就不能买啦. #include<cstdio> #inclu ...

  5. golang thrift 源码分析,服务器和客户端究竟是如何工作的

    首先编写thrift文件(rpcserver.thrift),运行thrift --gen go rpcserver.thrift,生成代码 namespace go rpc service RpcS ...

  6. ASP.NET MVC 中将数据从View传递到控制器中的三种方法(表单数据绑定)

    http://www.cnblogs.com/zyqgold/archive/2010/11/22/1884779.html 在ASP.NET MVC框架中,将视图中的数据传递到控制器中,主要通过发送 ...

  7. Spring学习4-面向切面(AOP)之Spring接口方式

    一.初识AOP    关于AOP的学习可以参看帮助文档:spring-3.2.0.M2\docs\reference\html目录下index.html的相关章节       1.AOP:Aspect ...

  8. MySQL: Starting MySQL….. ERROR! The server quit without updating PID file解决办法

    MySQL: Starting MySQL….. ERROR! The server quit without updating PID file解决办法 1 问题 [root@localhost m ...

  9. Machine Learning Methods: Decision trees and forests

    Machine Learning Methods: Decision trees and forests This post contains our crib notes on the basics ...

  10. Dedecms最新版本存储型XSS

    由于编辑文章的模板参数 typeid2可控,导致存储XSS发生. dedecms/dede/templets/article_edit.htm页面316-325行代码如下: <tr> &l ...