#1111 - Invalid use of group function in mysql

我怕爱的太早我们不能终老 提交于 2021-01-27 20:56:16

问题


I have a table M_DAILY with fields

PS_DATE date,
tp int,
ep int,
mp int,

and have a working version of a user defined function nvl(x,y) which returns x if not null and y if x is null

My MySQL query is-

select sum(avg(case date_format(PS_DATE,'%Y') when '2005' then nvl(tp,0) else 0 end))tp, sum(avg(case date_format(PS_DATE,'%Y') when '2005' then nvl(ep,0) else 0 end)) ep, sum(avg(case date_format(PS_DATE,'%Y') when '2005' then nvl(mp,0) else 0 end)) mp
from M_DAILY
where PS_DATE >= date ('2005-01-01') and PS_DATE <= date ('2005-12-31')
group by PS_DATE;

I get the following error

#1111 - Invalid use of group function in mysql

Please help.


回答1:


SUM(), COUNT(), AVG(), MIN(), MAX(), etc. are aggregate functions that requires you to specify a GROUP BY, unless you're using them on every column in your SELECT-list.

Remove Group By Clause

Try this

SELECT SUM(tp),SUM(ep),SUM(mp) FROM
(
    SELECT Avg(case date_format(PS_DATE,'%Y') when '2005' then nvl(tp,0) else 0 end) tp,   
           Avg(case date_format(PS_DATE,'%Y') when '2005' then nvl(ep,0) else 0 end) ep, 
           Avg(case date_format(PS_DATE,'%Y') when '2005' then nvl(mp,0) else 0 end) mp   
    FROM M_DAILY 
    WHERE PS_DATE >= date ('2005-01-01') and PS_DATE <= date ('2005-12-31');
) As T


来源:https://stackoverflow.com/questions/22828569/1111-invalid-use-of-group-function-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!