我有一个带有字段的表M_DAILY
PS_DATE date,
tp int,
ep int,
mp int,
并且具有用户定义的function nvl(x,y)
的工作版本,如果不为null,则返回x,如果x为null,则返回y
我的MySQL查询是 -
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;
我收到以下错误
#1111 - Invalid use of group function in mysql
请帮忙。
答案 0 :(得分:1)
SUM(),COUNT(),AVG(),MIN(),MAX()等是聚合函数 要求您指定GROUP BY,除非您每次都使用它们 SELECT列表中的列。
删除分组依据条款
试试这个
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