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.
Author |
Topic |
PGG_CA
Starting Member
24 Posts |
Posted - 2012-06-14 : 11:39:39
|
I have these tables and fields:TableAfields: id, totaltimeTableBfields: id, time_minSample Data:TableA100, NULL101, NULL102, NULLTableB100, 5100, 10101, 12101, 4101, 6102, 2I have this query that would update the totaltime field in TableA with a sum of the time_min for each ID. TableA, after the update query, should now look like this:TableA100, 15101, 22102, 2UPDATE aSET a.totaltime = (SELECT SUM(TableB.time_min) FROM TableB inner join TableA on TableB.id = TableA.id GROUP BY TableA.id)FROM TableA a inner join TableB b on a.id=b.idWhen I execute it, I get the error: Subquery returned more than 1 value. This not permitted when the subquery follows =, ... or when the subquery i used as an expression.How do I accomplish what I want to do? |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-06-14 : 12:00:49
|
update tableAset totaltime = (select sum(time_min) from TableB b where a.id = b.id)from TableA a==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-06-14 : 12:01:20
|
malformed. There are many different ways.Here using a derived table UPDATE a SET [totalTime] = sumb.[total]FROM TableA AS a JOIN ( SELECT b.[id] AS [id] , SUM(b.[Time_min]) AS [total] FROM TableB AS b GROUP BY b.[id] ) AS sumb ON sumb.[id] = a.[ID] Transact CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. |
 |
|
|
|
|
|
|