0

我目前正在做这个查询:

SELECT Date_Format(M.Signupdate,"%Y-%m") as YearMonth,
SUM(IF(Count(H.Nr) >= 0,1,0)) ,
SUM(IF(Count(H.Nr) >= 1,1,0)) ,
SUM(IF(Count(H.Nr) >= 2,1,0)) ,
SUM(IF(Count(H.Nr) >= 3,1,0)) 
FROM people M 
INNER JOIN History H ON H.login = M.Login 
WHERE
M.Masteraccount = "" AND
M.logincount > 5 AND
DATEDIFF(M.lastlogin,M.Signupdate) >= 3 AND
DATEDIFF(H.EntryDate,M.Signupdate) <= 151
GROUP BY YearMonth ORDER BY YearMonth ;

但是,我不断收到“[Err] 1111 - 组函数的无效使用”错误。如果我删除 SUM,它就可以正常工作。请问有人给我答案吗?

4

1 回答 1

1

您不能在GROUP BY. 您需要将 放置在Date_Format(M.Signupdate,"%Y-%m")GROUP BY才能使其正常工作。此外,您不能嵌套聚合函数,因此您需要使用子查询来获取count,然后使用sum

select YearMonth,
    SUM(IF(CountNR >= 0,1,0)) ,
    SUM(IF(CountNR >= 1,1,0)) ,
    SUM(IF(CountNR >= 2,1,0)) ,
    SUM(IF(CountNR >= 3,1,0)) 
from
(
    SELECT Date_Format(M.Signupdate,"%Y-%m") as YearMonth,
        Count(H.Nr) CountNR 
    FROM people M 
    INNER JOIN History H 
      ON H.login = M.Login 
    WHERE M.Masteraccount = "" AND
      M.logincount > 5 AND
      DATEDIFF(M.lastlogin,M.Signupdate) >= 3 AND
      DATEDIFF(H.EntryDate,M.Signupdate) <= 151
    GROUP BY Date_Format(M.Signupdate,"%Y-%m") 
) src
GROUP BY YearMonth
ORDER BY YearMonth;
于 2013-01-02T15:15:10.037 回答