8

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)
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
Ranjit Kumar
  • 779
  • 2
  • 8
  • 20

2 Answers2

11

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
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
2

I did update the above query based on above answer but there is an issue.

UPDATE tmpTotal t1
    INNER JOIN 
    (
         SELECT thirdlevel_delivery_id, MAX(baseline_id) MaxBaseLineId
         FROM tmpTotal
         GROUP BY release_id
    ) t2 ON t1.release_id = t2.release_id
    SET t1.planned_delivery_date = t2.MaxBaseLineId;

It gives

Error Code : 1054
Unknown column 't2.release_id' in 'on clause'
AKS
  • 1,279
  • 11
  • 27
  • 1
    You have to select the column `release_id` in the statment called `t2` in order to use it in the join clause. – Kobi Nov 12 '13 at 16:00