mysql - 两个聚合函数的Mysql总和不起作用
问题描述
您好我想添加两个聚合函数的结果,但我得到“组函数的无效使用”。任何人都可以更正以下查询:
SELECT mc.complaint_type_id,
mc.complaint_type,
sum(sum(case when c.is_solved = 1 then 1 else 0 end) + sum(case when ((c.is_solved = 0) and (c.res_user_id is null)) then 1 else 0 end)) as complaints_count,
from svk_apt_master_complaints mc
left join svk_apt_complaints c on c.complaint_type_id = mc.complaint_type_id and c.is_active = 1
and c.customer_id = 1 and c.association_id = 1
group by mc.complaint_type_id
解决方案
尝试这个:
SELECT mc.complaint_type_id,
mc.complaint_type,
sum(case when c.is_solved = 1 then 1 else 0 end) + sum(case when ((c.is_solved = 0) and (c.res_user_id is null)) then 1 else 0 end) as complaints_count
from svk_apt_master_complaints mc
left join svk_apt_complaints c on c.complaint_type_id = mc.complaint_type_id
where c.is_active = 1 and c.customer_id = 1 and c.association_id = 1
group by mc.complaint_type_id, mc.complaint_type
sum()
使用运算符时不需要+
。另外,SUM
是聚合函数。
此外,您选择未包含在聚合中的列:mc.complaint_type
。您需要将其包含在其中group by
还是仅将其删除。
推荐阅读
- clips - 如何使用 PyCLIPS 进行交互式会话?
- c - 如何在C中实现算术右移
- php - 这个 ~* 语法在 sql 查询中的作用是什么?
- javascript - 使用 ajax 处理的多个表单 - 提交按钮 ID
- elixir - 部分覆盖部分
- javascript - MongoDB将多个数组中的元素添加到根元素
- batch-file - 从子文件夹复制数据的批处理脚本
- ibm-content-navigator - Content Navigator 打开文件夹仅获得 200 个结果
- python - 如何在不使用 Python 中的文件路径的情况下读取文件
- python - 如何根据熊猫中的给定条件更改特定行中的值?