| Author |
Topic |
|
DavidChel
Constraint Violating Yak Guru
474 Posts |
Posted - 2008-10-10 : 16:16:13
|
I have a table that looks likething like: identity_column fcpoptext fnorder100000 FED EX 1100001 DHL 2100002 UPS 3 What I need to do is to re-order the fnorder as consecutive nubmers starting with 0 based on the alphabetical order of fcpoptext. I can do update statements, but I don't really know how to do so incrementally. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-10 : 16:57:40
|
| Hello David,maybe like this (sql server 2005):create table #sample(identity_column int,fcpoptext varchar(10),fnorder int)insert #sampleselect 100000, 'FED EX', 1 unionselect 100001, 'DHL', 2 unionselect 100002, 'UPS', 3-- this select shows, how to get the wanted valueselectidentity_column,fcpoptext,fnorder,row_number() over (order by fcpoptext)-1 as new_fnorderfrom #sampledrop table #sampleGreetingsWebfredPlanning replaces chance by mistake |
 |
|
|
DavidChel
Constraint Violating Yak Guru
474 Posts |
Posted - 2008-10-10 : 16:58:23
|
| Thanks Fred. I'm sorry, but I should have mentioned that I am on SQL 2000. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-10 : 17:12:48
|
| maybe it's possible to take first a temp table and the use it to update?create table #sample(identity_column int,fcpoptext varchar(10),fnorder int)insert #sampleselect 100000, 'FED EX', 1 unionselect 100001, 'DHL', 2 unionselect 100002, 'UPS', 3-- this select shows, how to get the wanted value (2005)selectidentity_column,fcpoptext,fnorder,row_number() over (order by fcpoptext)-1 as new_fnorderfrom #sample---- this select-into shows, how to get the wanted value (2000)selectidentity_column,fcpoptext,identity(int,0,1) as new_fnorder_2000,fnorderinto #new_samplefrom #sampleorder by fcpoptextselect * from #new_sampledrop table #sampledrop table #new_sampleFredPlanning replaces chance by mistake |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-11 : 01:04:45
|
| [code]UPDATE tSET t.fnorder=(select count(*) from YourTable WHERE fcpoptext<t.fcpoptext)FROM YourTable t[/code] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-13 : 03:47:21
|
quote: Originally posted by sql1_learner sdfssgsdgsd
Touché! E 12°55'05.63"N 56°04'39.26" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-13 : 03:47:43
|
quote: Originally posted by sql1_learner sdfssgsdgsd
what does that mean? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-13 : 03:49:36
|
If you have the clustered index over fcPopText, this will be really fast for many recordsDECLARE @Index INTSET @Index = -1UPDATE t1SET t1.fnOrder = t1.fnOrder = @Index + 1FROM Table1 AS t1 WITH (TABLOCK, INDEX(0)) E 12°55'05.63"N 56°04'39.26" |
 |
|
|
DavidChel
Constraint Violating Yak Guru
474 Posts |
Posted - 2008-10-13 : 09:06:37
|
quote: Originally posted by visakh16
UPDATE tSET t.fnorder=(select count(*) from YourTable WHERE fcpoptext<t.fcpoptext)FROM YourTable t
Amazing, that worked perfectly. Thanks again Visahk. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-13 : 09:31:01
|
quote: Originally posted by DavidChel
quote: Originally posted by visakh16
UPDATE tSET t.fnorder=(select count(*) from YourTable WHERE fcpoptext<t.fcpoptext)FROM YourTable t
Amazing, that worked perfectly. Thanks again Visahk. 
welcome David |
 |
|
|
|