Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid use of group function while updating a table with sum function

I have two tables: o_daily_lcsgeneration and o_daily_generation.

While trying to update the o_daily_generation I receive an error saying:

error(1111) invalid use of Group function

Here is the code I am running:

update o_daily_generation join o_daily_lcsgeneration 
on o_daily_generation.Location =o_daily_lcsgeneration.Location 
   and o_daily_generation.Date =o_daily_lcsgeneration.Date  
set o_daily_lcsgeneration.Turbine_Generation =sum(o_daily_generation.Turbine_Generation)
like image 699
Ranjit Kumar Avatar asked Nov 03 '12 07:11

Ranjit Kumar


People also ask

How do you fix invalid use of a group function?

Fix invalid use of group function with a subquery When you need to use an aggregate function inside a WHERE clause, you need to wrap the aggregate function call in a subquery. That's one way you can fix the invalid use of group function error.

Can we use function in update statement?

No, you cannot.


1 Answers

Try this instead:

UPDATE o_daily_generation od
INNER JOIN 
(
     SELECT Location, SUM(Turbine_Generation) TurbineSum
     FROM o_daily_lcsgeneration
     GROUP BY Location
) og ON od.Location = og.Location
SET od.Turbine_Generation = og.TurbineSum
like image 92
Mahmoud Gamal Avatar answered Oct 26 '22 18:10

Mahmoud Gamal