繁体   English   中英

#1111-无效使用组函数-MYSQL

[英]#1111 - Invalid use of group function - MYSQL

好吧,我正在使用以下查询,但是我收到以下错误(#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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM