all groups > sql server programming > november 2007 >
You're in the

sql server programming

group:

Partition No. in Partition BY


Partition No. in Partition BY nkg
11/2/2007 10:00:39 AM
sql server programming:
Is it possible to accomplish following.

create table Orders

(

RowID int identity,

Batchid VARCHAR(10),

Product varchar(10)

)

go

insert into [orders](BatchID,Product)

values('10X','Cycle')

insert into [orders](BatchID,Product)

values('10X','Book')

insert into [orders](BatchID,Product)

values('10X','Phone')



insert into [orders](BatchID,Product)

values('20Y','Paper')

insert into [orders](BatchID,Product)

values('20Y','Monitor')

insert into [orders](BatchID,Product)

values('20Y','Pencil')

go

select row_number() over (PARTITION BY BATCHID order by RowID)[Seq],BatchID
from [Orders]

go



Seq BatchID
-------------------- ----------
1 10X
2 10X
3 10X
1 20Y
2 20Y
3 20Y


how can i achive following result

Seq BatchID GroupNo.
-------------------- ------------------ -------------
1 10X 1
2 10X 1
3 10X 1
1 20Y 2
2 20Y 2
3 20Y 2


Re: Partition No. in Partition BY nkg
11/2/2007 10:18:14 AM
thx. a lot.

[quoted text, click to view]

Re: Partition No. in Partition BY Dieter Noeth
11/2/2007 4:13:49 PM
[quoted text, click to view]

select
row_number() over (PARTITION BY BATCHID order by RowID)[Seq],
BatchID,
dense_rank() over (order BY BATCHID)
from [Orders]

AddThis Social Bookmark Button