Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Insert into

Author  Topic 

davey85uk
Starting Member

2 Posts

Posted - 2012-04-03 : 10:15:06
Hello, can someone help, i am trying to sum the values of a table with a RecordID. Then take that total value and insert into another table.
See code below;

INSERT INTO OEETable(MachineDowntime)
VALUES ((SELECT SUM(Time) AS Expr1 FROM MachineDowntime WHERE (RecordID = 2))
WHERE RecordID=2

It doesnt like the second WHERE statement at all, iam using SQL Express 2008 R2. Thanks in advance.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-04-03 : 10:24:13
[code]
INSERT INTO OEETable(MachineDowntime)
SELECT SUM(Time) AS Expr1
FROM MachineDowntime
WHERE RecordID = 2
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-04 : 18:28:17
is it only for RecordID=2 you want to do this or do you want to repeat it for each RecordID value?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

davey85uk
Starting Member

2 Posts

Posted - 2012-04-05 : 05:12:12
Hi, thanks for the responses,
The RecordID field exists in the MachineDowntime Table and the OEE table and is linked as a primary key. Hence the problem being it doesnt like writing the value '2' to the second WHERE statement.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-04-05 : 06:52:44
khtan has given the solution.
visakh was asking if it is enough for you to do that stuff only for RecordID 2 or maybe you want a select with gives sum of time grouped by each RecordID...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-04-05 : 06:54:16
so you can make it more clear by giving table structure, sample data and wanted result!


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -