0

Im new to SQL. I have a table employee, it has fields eID and eLoad. In addition i have table ongoing_projects it has pID, eID (those two are primary) and eLoad. Im trying to insert a sum of all eLoads for each employee. I have an idea for pseudocode, but I cannot implement it. Any Ideas? Thank you!

For each eID in table employee
DO
UPDATE `employee` SET eload=(
  SELECT SUM (eload) as eload
  FROM ongoing_projects 
);
Dorian
  • 22,759
  • 8
  • 120
  • 116

1 Answers1

2

If I understood It, you would like to do something like:

UPDATE employee e
SET e.eLoad = (SELECT SUM(op.eLoad) FROM ongoing_projects op WHERE op.eID=e.eID);

It updates each row in employees.eLoad column with the sum of the ongoing_projects.eLoad where the ongoing_projects.eID=actual employee eID

Or if you would like to SUM employees.eLoad with the ongoing_projects eLoad then the query may look:

UPDATE employee e
SET e.eLoad = e.eLoad + (SELECT SUM(op.eLoad) FROM ongoing_projects op WHERE op.eID=e.eID);
user3714582
  • 1,830
  • 13
  • 22
  • Thank you! Just to be clear ( as I said I'm new to it) when I run your first querry - if i have employee with eID 111 it will set into employee.eload the sum of eload from projects where eID = 111, and it will be done for each eID in employee??? – user3578048 Jun 14 '14 at 16:00
  • Yes this does this what you described – user3714582 Jun 14 '14 at 18:19