gpt4 book ai didi

两个聚合函数的Mysql总和不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 10:33:44 26 4
gpt4 key购买 nike

您好,我想添加两个聚合函数的结果,但我得到“组函数的无效使用”。任何人都可以更正以下查询:

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 中还是仅将其删除。

关于两个聚合函数的Mysql总和不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52624160/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com