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
 How to insert sequence number with group

Author  Topic 

awalsql
Starting Member

11 Posts

Posted - 2008-06-11 : 18:34:34
SOURCE TABLE
ID________COMMENT
123_______I am joe
123_______I am programmer
124_______I am Wang
124_______I am programmer
124_______I like cricket

DESTINATION TABLE

ID_____SEQ______COMMENT
123_____1_______I am joe
123_____2_______I am programmer
124_____1_______I am wang
124_____2_______I am programmer
124_____3_______I like cricket
can somebody please advise the easiest way to do this in sql 2000?

awalsql
Starting Member

11 Posts

Posted - 2008-06-11 : 22:48:38
Guys, this problem is solved. Below is the code I found.Thanks
Create
Table SRC

(

no
int identity(1,1) ,

id
int,

comment
varchar(20)

)

Insert
Into SRC Values (123,'I am Joe')

Insert
Into SRC Values (123,' I am programmer')

Insert
Into SRC Values (124,'I am Wang')

Insert
Into SRC Values (124,' I am programmer')

Insert
Into SRC Values (124,'I like Criket')



Create
Table DEST

(

id
int,

seq
int,

comment
varchar(20)

)

Declare
@seq int

Declare
@id int

Declare
@comment varchar(20)

Declare
@seqmax int

Declare
@nomax int

Declare
@no int

Set
@id = 123

Set
@seq = 1

While
(@id<=124)

Begin

Select @seqmax = count(*) from SRC Where id = @id

Select @no = min(no) from SRC Where id = @id

Select @nomax =max(no) from SRC Where id = @id

Select @id =id,@comment=comment From SRC Where id = @id and no =@no

While(@no<=@nomax)

Begin

Insert Into DEST

Select @id,@seq,comment From SRC Where id = @id and no =@no

Set @no = @no + 1

Set @seq =@seq + 1

End

Set @seq = 1

Set @id =@id +1

End

Select
* From DEST
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-12 : 00:54:20
Do you have any other column in your table with unique values?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-12 : 09:52:31
You can also do

select id, (select count(*) from src where id=t.id and no<=t.no) as no, comment
from src t order by 1,2



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -