嗯,我正在使用以下查询,但是我收到以下错误(#1111 - 组函数的无效使用)。这是查询:
SELECT
a.`wo_number`,
a.`crew_est`,
a.`manhour_est`,
b.`act_hours`,
a.`status`,
SUM(CASE
WHEN a.`status` = 'FINISH' THEN SUM(a.`status`)
ELSE 0
END) / SUM(a.`crew_est` * a.`manhour_est`) AS `total_percentage`
FROM
`table_a` AS a
LEFT JOIN
`table_b` AS b ON a.`wo_number` = b.`wo_number`
WHERE
a.`wo_number` = 'some_number'
GROUP BY a.`wo_number` , a.`sheet` , a.`serial`
如果我删除 CASE WHEN THEN 内的 SUM(),则查询有效。但是,我没有得到满足我需要的准确结果。
任何帮助将不胜感激。 谢谢。
最佳答案
您正在嵌套 SUM 函数,这是不允许的。
您可能想要这个:
SELECT
a.`wo_number`,
a.`crew_est`,
a.`manhour_est`,
b.`act_hours`,
a.`status`,
SUM(CASE
WHEN a.`status` = 'FINISH' THEN 1 -- or the column you want to aggregate
END) / SUM(a.`crew_est` * a.`manhour_est`) AS `total_percentage`
FROM
`table_a` AS a
LEFT JOIN
`table_b` AS b ON a.`wo_number` = b.`wo_number`
WHERE
a.`wo_number` = 'some_number'
GROUP BY a.`wo_number` , a.`sheet` , a.`serial`
另外,简写
SUM(CASE
WHEN a.`status` = 'FINISH' THEN 1
END)
是
SUM(a.`status` = 'FINISH')
关于mysql - #1111 - 组函数使用无效 - MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41726502/