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
 sno help in sql 2000

Author  Topic 

gfaryd
Starting Member

27 Posts

Posted - 2011-07-18 : 12:14:20
dear all
i have following table

create table test (id int, batch varchar(5))

insert into test values (1,abc)
insert into test values (1,abc)
insert into test values (2,abc)
insert into test values (2,abc)
insert into test values (3,abc)
insert into test values (1,xyz)
insert into test values (1,xyz)
insert into test values (2,xyz)
insert into test values (2,xyz)
insert into test values (3,xyz)
insert into test values (1,pqr)
insert into test values (2,pqr)
insert into test values (2,pqr)


i want to assign sno to this data on following criteria

sno id batch
1 1 abc
2 1 abc
3 2 abc
4 2 abc
5 3 abc
1 1 xyz
2 1 xyz
3 2 xyz
4 2 xyz
5 3 xyz
1 1 pqr
2 2 pqr
3 2 pqr


that sno restarts when batch no changes and sno restat from min id no

how to apply that


regards

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2011-07-18 : 13:57:05
This solution is not nearly as efficient as the 2005 (and greater) ranking functions but it is one way:

create table #t (ident int identity(1,1), id int, batch varchar(5))
insert #t (id, batch)
select id, batch
from test
order by batch, id

select sno = t.ident - (select min(z.ident) from #t z where z.batch = t.batch) + 1
,id
,batch
from #t t

OUTPUT:
sno id batch
----------- ----------- -----
1 1 abc
2 1 abc
3 2 abc
4 2 abc
5 3 abc
1 1 pqr
2 2 pqr
3 2 pqr
1 1 xyz
2 1 xyz
3 2 xyz
4 2 xyz
5 3 xyz


Be One with the Optimizer
TG
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-07-19 : 05:24:15
Alternatively if you use front end application to show data, you can do numbering there

Madhivanan

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

- Advertisement -