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
 sql query

Author  Topic 

mana
Posting Yak Master

102 Posts

Posted - 2014-11-03 : 10:13:23
Hello

I have the following code

update AMPPU_Alle_Fehlteile
set [Summe von Reparatur_min]=
(SELECT SUM([Reparatur-min])
from (select [Reparatur-min],Fehlteil,Offline_date
FROM AMPPU_Alle_Fehlteile b
WHERE Fehlteil=AMPPU_Alle_Fehlteile.Fehlteil
) a GROUP BY a.Offline_date,a.Fehlteil );

nad i have the following error but i don't know why. can you help me pleasse??

Thank you

The subquery has returned more than one value. This is not permitted when the subquery to =,! =, <, <=,> Or> = follows or is used as an expression.

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-11-03 : 11:22:05
I mocked up your scenario with some temporary tables. Hopefully you can apply it to your actual situation:


declare @t table (a int, b int, d date, bsum int)
insert into @t(a,b, d) values
(1, 2, cast(getdate() as date))
,(1, 3, cast(getdate() as date))
,(3,4,cast(getdate() as date))


update t
set bsum = t1.bsum

from @t t
join (
select a, sum(b)
from @t
group by a) t1(a, bsum)
on t1.a = t.a

select * from @t



Note that your multiple nested subqueries can be a performance killer. Look for the simplest approach first.
Go to Top of Page

mana
Posting Yak Master

102 Posts

Posted - 2014-11-04 : 04:49:14
thank you for the response
it works


quote:
Originally posted by gbritton

I mocked up your scenario with some temporary tables. Hopefully you can apply it to your actual situation:


declare @t table (a int, b int, d date, bsum int)
insert into @t(a,b, d) values
(1, 2, cast(getdate() as date))
,(1, 3, cast(getdate() as date))
,(3,4,cast(getdate() as date))


update t
set bsum = t1.bsum

from @t t
join (
select a, sum(b)
from @t
group by a) t1(a, bsum)
on t1.a = t.a

select * from @t



Note that your multiple nested subqueries can be a performance killer. Look for the simplest approach first.

Go to Top of Page
   

- Advertisement -