博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Django 如何实现 如下 联表 JOIN 查询?
阅读量:5275 次
发布时间:2019-06-14

本文共 4078 字,大约阅读时间需要 13 分钟。

SQL语句:select distinct a.device_hash, sum(b.cmn_merge_count) from (select distinct device_hash from tbl_fileprotect_svc_instance where customer_id='3f500ac5-020d-3ce3-a2a2-51a59ddd606e') as a left join tbl_file_protect_alarm as b on a.device_hash = b.device_hash group by a.device_hash;简化SQL如下(把子查询转化为WHERE语句):复制代码select distinct a.device_hash, sum(b.cmn_merge_count) from tbl_fileprotect_svc_instanceas a left join tbl_file_protect_alarm as b on a.device_hash = b.device_hash where a.customer_id='3f500ac5-020d-3ce3-a2a2-51a59ddd606e'group by a.device_hash;select distinct a.device_hash, sum(b.cmn_merge_count) from  tbl_fileprotect_svc_instance as a left join tbl_file_protect_alarm as b on a.device_hash = b.device_hash where a.customer_id='3f500ac5-020d-3ce3-a2a2-51a59ddd606e' group by a.device_hash;select distinct a.device_hash, sum(b.cmn_merge_count) as alarm_sum from  tbl_fileprotect_svc_instance as a left join tbl_file_protect_alarm as b on a.device_hash = b.device_hash where a.customer_id='3f500ac5-020d-3ce3-a2a2-51a59ddd606e' group by a.device_hash;复制代码 Django代码:复制代码TODO:方法一:ret = []device_list = models.FILE_PROTECT_INSTANCE.objects.filter(customer_id=customer_id)for item in device_list:    tmp_dict = {}    tmp_dict['device_hash'] = item.device_hash    tmp_dict['hostname'] = item.hostname    tmp_dict['status'] = item.status     from django.db.models import Sum    alarm_sum_group_items = models.FILE_PROTECT_ALARM.objects.filter(customer_id=customer_id).filter(device_hash=tmp_dict['device_hash']).values('device_hash').annotate(alarm_sum=Sum('cmn_merge_count'))    #tmp_dict['customer_id'] = customer_id    tmp_dict['alarm_sum'] = alarm_sum_group_items[0]['alarm_sum'] if (len(alarm_sum_group_items)!=0) else 0    ret.append(tmp_dict)复制代码 方法二:hashes = A.objects.values_list("device_hash", flat=True).filter(customer_id="3f500ac5-020d-3ce3-a2a2-51a59ddd606e")B.objects.filter(device_hash__in=hashes).values_list("device_hash").annotate(Sum("cmn_merge_count")) SQL实现聚合查询统计(Sum,Count等)复制代码from django.db.models import Sum        #start_time = datetime.utcfromtimestamp(data['start_time']).replace(tzinfo=utc)        #end_time = datetime.utcfromtimestamp(data['end_time']).replace(tzinfo=utc)        end_time = datetime.now()        start_time = end_time + timedelta(days=-1)        condition['cmn_alert_time__range'] = (start_time, end_time)        #alarm_sum_group_items_day = models.FILE_PROTECT_ALARM.objects.filter(**condition).values('customer_id').annotate(alarm_sum_day=Sum('cmn_merge_count'))        alarm_sum_day = models.FILE_PROTECT_ALARM.objects.filter(**condition).aggregate(Sum("cmn_merge_count"))        day_sum = alarm_sum_day['cmn_merge_count__sum'] if (alarm_sum_day['cmn_merge_count__sum'] is not None) else 0                 end_time = datetime.now()        start_time = end_time + timedelta(days=-7)        condition['cmn_alert_time__range'] = (start_time, end_time)        #alarm_sum_group_items_week = models.FILE_PROTECT_ALARM.objects.filter(**condition).values('customer_id').annotate(alarm_sum_week=Sum('cmn_merge_count'))        alarm_sum_week = models.FILE_PROTECT_ALARM.objects.filter(**condition).aggregate(Sum("cmn_merge_count"))        week_sum = alarm_sum_week['cmn_merge_count__sum'] if (alarm_sum_week['cmn_merge_count__sum'] is not None) else 0                 condition = {}        #condition['device_hash'] = data['device_hash']        condition['customer_id'] = customer_id        end_time = datetime.now()        #start_time = end_time + timedelta(days=-7)        condition['cmn_alert_time__lte'] = end_time        #alarm_sum_group_items_all = models.FILE_PROTECT_ALARM.objects.filter(**condition).values('customer_id').annotate(alarm_sum_all=Sum('cmn_merge_count'))        alarm_sum_all = models.FILE_PROTECT_ALARM.objects.filter(**condition).aggregate(Sum("cmn_merge_count"))        all_sum = alarm_sum_all['cmn_merge_count__sum'] if (alarm_sum_all['cmn_merge_count__sum'] is not None) else 0 参考资料:http://stackoverflow.com/questions/6481279/django-sum-query

  

转载于:https://www.cnblogs.com/pyxiaomangshe/p/7741612.html

你可能感兴趣的文章
CF219D Choosing Capital for Treeland
查看>>
杂七杂八的小笔记本
查看>>
51Nod1353 树
查看>>
CF1215E Marbles
查看>>
BZOJ2339 HNOI2011卡农(动态规划+组合数学)
查看>>
octave基本操作
查看>>
axure学习点
查看>>
WPF文本框只允许输入数字[转]
查看>>
dom4j 通用解析器,解析成List<Map<String,Object>>
查看>>
第一个项目--用bootstrap实现美工设计的首页
查看>>
使用XML传递数据
查看>>
TYVJ.1864.[Poetize I]守卫者的挑战(概率DP)
查看>>
0925 韩顺平java视频
查看>>
iOS-程序启动原理和UIApplication
查看>>
mysql 8.0 zip包安装
查看>>
awk 统计
查看>>
模板设计模式的应用
查看>>
实训第五天
查看>>
平台维护流程
查看>>
2012暑期川西旅游之总结
查看>>