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 |
|
bismarkcount
Starting Member
15 Posts |
Posted - 2009-01-13 : 15:06:15
|
| I want to update a table with the sum of some other rows in the same table. example:I want to update 1t in 2008 with the sum of all key 1 in 2008, the 2t with the sum of all key 2 in 2008, etc...the same for 2009.how would it be the statement to do this?year key amnt2008 1 502008 1 452008 2 602008 3 552008 1t 02008 2t 02008 3t 02009 1 402009 1 302009 2 552009 1t 02009 2t 0any suggestions??thanx in advance |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-01-13 : 15:13:47
|
| [CODE]UPDATE TSET T.AMNT = A.AMNTFROM MY_TABLE T JOIN (SELECT YEAR, (CASE KEY WHEN 1 THEN '1t' WHEN 2 THEN '2t' WHEN 3 THEN '3t' END) CONV_KEY, SUM(AMNT) AS AMNT FROM MY_TABLE GROUP BY YEAR,(CASE KEY WHEN 1 THEN '1t' WHEN 2 THEN '2t' WHEN 3 THEN '3t' END)) A ON T.YEAR = A.YEAR AND T.KEY = A.CONV_KEY[/CODE] |
 |
|
|
Skorch
Constraint Violating Yak Guru
300 Posts |
Posted - 2009-01-13 : 15:18:52
|
| It's a terrible design to store sums and values in the same column of a table. |
 |
|
|
bismarkcount
Starting Member
15 Posts |
Posted - 2009-01-13 : 15:23:38
|
| rohitkumar: COOL QUERYSkorch: THNX... YOU'RE RIGHT |
 |
|
|
|
|
|