组功能的使用无效(在select中使用count)

时间:2013-03-05 07:27:03

标签: mysql sql mysql-error-1138


我在我的SQL查询中输出了一个错误输出,注释“无效使用组功能”

这是我的查询

SELECT * FROM 
(SELECT project,tbl_nif.POno,tbl_custmon.so, tbl_nif.matcode, tbl_nif.prodesc, 
    COUNT(CASE WHEN WEEK(MAX(plannedGI))+1=6 THEN 1 ELSE NULL END) as `week 6`,
    COUNT(CASE WHEN WEEK(MAX(plannedGI))+1=2 THEN 1 ELSE NULL END) as `week 2`,
    COUNT(CASE WHEN WEEK(MAX(plannedGI))+1=3 THEN 1 ELSE NULL END) as `week 3`,
    COUNT(CASE WHEN WEEK(MAX(plannedGI))+1=4 THEN 1 ELSE NULL END) as `week 4` 
FROM tbl_nif,tbl_custmon,tbl_incoming 
WHERE tbl_nif.so=tbl_custmon.so AND tbl_nif.bill='521513' AND tbl_incoming.shipment!=tbl_custmon.shipment 
            AND plannedGI!='0000-00-00' 
GROUP BY tbl_custmon.so 
ORDER BY tbl_custmon.so) AS grid 
ORDER BY project ASC

如果我在查询中不包含“count”,它将显示输出。

所以,请告诉我这个问题的想法。
非常感谢!

结论
将“Max”函数放在查询上会产生一个自然的“分组依据”。
所以我把“Max”函数与其他SELECT字段内联。
所以查询将是这样的

SELECT * FROM 
(SELECT project,tbl_nif.POno,tbl_custmon.so, tbl_nif.matcode, tbl_nif.prodesc, MAX(plannedGI),
    COUNT(CASE WHEN WEEK(plannedGI)+1=6 THEN 1 ELSE NULL END) as `week 6`,
    COUNT(CASE WHEN WEEK(plannedGI)+1=2 THEN 1 ELSE NULL END) as `week 2`,
    COUNT(CASE WHEN WEEK(plannedGI)+1=3 THEN 1 ELSE NULL END) as `week 3`,
    COUNT(CASE WHEN WEEK(plannedGI)+1=4 THEN 1 ELSE NULL END) as `week 4`
FROM tbl_nif,tbl_custmon,tbl_incoming 
WHERE tbl_nif.so=tbl_custmon.so AND tbl_nif.bill='521513' AND tbl_incoming.shipment!=tbl_custmon.shipment 
            AND plannedGI!='0000-00-00' 
GROUP BY tbl_custmon.so
ORDER BY tbl_custmon.so) AS grid 
ORDER BY project ASC

输出将显示我需要的数据。
非常感谢你:))

0 个答案:

没有答案