Showing posts with label Sequence number. Show all posts
Showing posts with label Sequence number. Show all posts

Sequence number within a group

Saturday, 10 April 2010

The following query can be used to generate the sequence numbers within a group.

select 
 GROUP_ID
, GROUP_COUNTER= ROW_NUMBER()
                                      OVER (  PARTITION BY [GROUP_ID]
                                                    ORDER BY [GROUP_ID]
                                                   ) 
from GROUP_TABLE

This query can generate a output like the following.

GROUP_ID       GROUP_COUNTER
-------------------------------------------
AAA                   1
AAA                   2
AAA                   3
BBB                    1
BBB                    2
CCC                   1