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
 update amount query help

Author  Topic 

pnpsql
Posting Yak Master

246 Posts

Posted - 2011-10-12 : 07:14:09

i have a table like :


id name amount type
1 a 120 x
2 a 120 x
3 c 120.41 y

i need to deduct amount equals to amount of a from name c. i need to use only quey not
t sql block pls help....

challenge everything

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-10-12 : 07:41:37
Not sure what you want

update tbl
set amount = amount - (select sum(amount) from tbl where name = a)
where name = c


==========================================
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.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-12 : 08:12:24
[code]
update t
set t.amount = t.amount - b.tot
from tbl t
cross apply (select sum(amount) as tot from tbl where name < t.name) b
[/code]

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

Go to Top of Page
   

- Advertisement -