Click here to Skip to main content
15,356,641 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Does anyone know how to sum a table in sql with multiple fields in.

For Example - Table 1 -
---------------------------------------------
|FactoryID | AreaID | ReasonCode | Time(Mins)|
|----------|--------|------------|-----------|
| 116      |  A1    |  14        | 12        |
| 116      |  A1    |  62        | 9         |


i want to add the time in Table 1

so

SQL =
SQL
Select SUM(Time(Mins)) where FactoryID ='116' and AreaID = 'A1'


and then pass this to Table 2 and get the results below

------------------------------------
|FactoryID |AreaID |TotalTime(Mins)|
------------------------------------
| 116      | A1    | 18            |


i have tried using

SQL
Insert Into Table2 (FactoryID,AreaID,TotalTime(Mins))
Select SUM(Time(Mins)) where FactoryID ='116' and AreaID = 'A1'


but to no avail

really sorry if this appears as highly basic, but any help or alternative approach would be welcome.
Posted
Updated 24-May-12 9:37am
v2

1 solution

Try
SQL
Insert into Table2 (FactoryID, AreaID, TotalTime(Mins)
Select FactoryID, AreaID, SUM(Time(Mins)) From Table1
Where FactoryID = '116' And AreaID = 'A1'
Group by FactoryID, AreaID

You have to give values for all fields in the insert (three fields in this case)
   
v2
Comments
Davey85 24-May-12 11:24am
   
Thanks very much, works perfect.
Maciej Los 24-May-12 11:54am
   
Good work, my 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900