好吧,我使用以下查询,但是我收到以下错误(#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(),则查询有效。但是,我无法得到准确的结果。
任何帮助将不胜感激。 谢谢。
答案 0 :(得分:0)
您正在嵌套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')