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 |
webforyou
Starting Member
15 Posts |
Posted - 2009-03-04 : 03:56:27
|
Dear all,I have a complex sql script to write "Re-number one column based on two column".It means I want to renumber the Product starting from 1 for the old minimum Product, then increase by 1 for other products. This is done just for each policy.Ex: My old data:Policy Product 100989 3100989 4100989 5100989 6100001 1100001 2100001 3201000 2201000 3201000 4201000 5201000 6201000 7201000 8My new data:100989 1100989 2100989 3100989 4100001 1100001 2100001 3201000 1201000 2201000 3201000 4201000 5201000 6201000 7 |
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-03-04 : 04:26:35
|
try thisdeclare @tab table(Policy int,Product int)insert into @tab select 100989,3insert into @tab select 100989,4insert into @tab select 100989,5insert into @tab select 100989,6insert into @tab select 100001,1insert into @tab select 100001,2insert into @tab select 100001,3insert into @tab select 201000,2insert into @tab select 201000,3insert into @tab select 201000,4insert into @tab select 201000,5insert into @tab select 201000,6insert into @tab select 201000,7insert into @tab select 201000,8select identity(int,1,1) as rid,* into #temp1 from @tab Select M.Policy,seqfrom(Select Policy,Product,(Select Count(*) from #temp1 Where Policy = Z.Policy and rid <= Z.rid ) as Seqfrom #temp1 Z)Mdrop table #Temp1 |
|
|
webforyou
Starting Member
15 Posts |
Posted - 2009-03-07 : 12:18:05
|
Many thanks for your code. It works as I want and better than if I use the fetch cursor approach. |
|
|
|
|
|
|
|