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 |
|
Amanda1980
Starting Member
3 Posts |
Posted - 2009-04-28 : 05:08:47
|
| Hi,I need to update a value in a table with the previos value+10 where two fields are used for reference. If there are no previos value then I only want to update Example:Tablenamn: Table_XColumns A, B, Ca b c123 147 10123 147 20123 147 0124 258 10124 258 0125 358 10126 369 0127 388 10UPDATE Table_XSET C=? WHERE (B <> 0 AND C = 0)I want to update every row where C=0 and B<>0 and A is the same for every row. If ther only is one instance of A then the value should be 10.After the update the table should look like this:a b c123 147 10123 147 20123 147 30124 258 10124 258 20125 358 10126 369 10127 388 10Thankfull for every thougt on this! |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-04-28 : 05:14:54
|
[code]declare @Table_X table( a int, b int, c int)-- Sample Data for testinginsert into @Table_Xselect 123, 147, 10 union allselect 123, 147, 20 union allselect 123, 147, 0 union allselect 124, 258, 10 union allselect 124, 258, 0 union allselect 125, 358, 10 union allselect 126, 369, 0 union allselect 127, 388, 10-- Query;with data (a, b, c, r)as( select a, b, c, r = row_number() over (partition by a, b order by case when c <> 0 then 1 else 2 end, c) from @Table_X)update dset c = r * 10from data dwhere c = 0select *from @Table_Xorder by a, b, c/* RESULTa b c ----------- ----------- ----------- 123 147 10123 147 20123 147 30124 258 10124 258 20125 358 10126 369 10127 388 10(8 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Amanda1980
Starting Member
3 Posts |
Posted - 2009-04-28 : 05:26:10
|
| Thanks! It looks a bit complex to me though. Can't figure out how I can apply this to my table when it has over one million rows and 30 columns.. Is it really applicable to me? Specially the select part:insert into @Table_Xselect 123, 147, 10 union allselect 123, 147, 20 union allselect 123, 147, 0 union allselect 124, 258, 10 union allselect 124, 258, 0 union allselect 125, 358, 10 union allselect 126, 369, 0 union allselect 127, 388, 10 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-04-28 : 05:50:26
|
you only need the query ;with data . . .the INSERT INTO @Table_X is for me to create sample data for testing KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Amanda1980
Starting Member
3 Posts |
Posted - 2009-04-28 : 08:05:12
|
Works like a charm, many thanks!! =Dquote: Originally posted by khtan you only need the query ;with data . . .the INSERT INTO @Table_X is for me to create sample data for testing KH[spoiler]Time is always against us[/spoiler]
|
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2009-04-28 : 08:37:42
|
quote: Originally posted by Amanda1980 Works like a charm, many thanks!! =Dquote: Originally posted by khtan you only need the query ;with data . . .the INSERT INTO @Table_X is for me to create sample data for testing KH[spoiler]Time is always against us[/spoiler]
But beware if the previous & next values are 0 you may have problems. |
 |
|
|
|
|
|
|
|