Mysql两个聚合函数之和不起作用
嗨,我想添加两个聚合函数的结果,但是我得到了“无效使用组函数”。任何人都可以更正以下查询:Mysql两个聚合函数之和不起作用,mysql,sum,aggregate,Mysql,Sum,Aggregate,嗨,我想添加两个聚合函数的结果,但是我得到了“无效使用组函数”。任何人都可以更正以下查询: 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))
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
是聚合函数
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
and c.is_active = 1
and c.customer_id = 1
and c.association_id = 1
GROUP BY mc.complaint_type_id, mc.complaint_type
此外,您还可以选择未包含在聚合中的列:mc.complaint\u type
。您需要将其包括在分组依据中,还是将其删除。尝试以下操作:
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
是聚合函数
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
and c.is_active = 1
and c.customer_id = 1
and c.association_id = 1
GROUP BY mc.complaint_type_id, mc.complaint_type
此外,您还可以选择未包含在聚合中的列:mc.complaint\u type
。您需要将其包括在“分组依据”中,还是仅将其删除。您需要在“分组依据”中指定“投诉类型”列
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 and c.is_active = 1 and c.customer_id = 1 and c.association_id = 1
group by mc.complaint_type_id,mc.complaint_type
您需要在group by中指定mc.complaint_type列
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 and c.is_active = 1 and c.customer_id = 1 and c.association_id = 1
group by mc.complaint_type_id,mc.complaint_type
删除嵌套的总和
并将mc.complaint\u type
添加到分组依据
子句中。如果需要添加两个聚合函数的值,请使用+
运算符,而不是聚合函数
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
and c.is_active = 1
and c.customer_id = 1
and c.association_id = 1
GROUP BY mc.complaint_type_id, mc.complaint_type
我还重新格式化了您的代码并删除了不必要的括号。删除嵌套的和
并将mc.complaint\u type
添加到分组依据
子句中。如果需要添加两个聚合函数的值,请使用+
运算符,而不是聚合函数
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
and 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(…)
。你想在这里实现什么?你不能像这样使用SUM(SUM(…)
。你在这里想要实现什么?