Adding Sequence Numbers

If you have a repeating group of data, such as multiple FAB descriptions for a part, you need to include a sequence number to each row in the group.

If your source data does not already have a sequence number, for example:

partcodeDescription
D1001FABFeature 1 for D1001
D1001FABFeature 2 for D1001
D1002FABFeature 1 for D1002

You can use this query to add a sequence number to each row in the group:

SELECT A.[part], A.[code], A.[Description],
  (SELECT Count(*) 
   FROM descr B 
   WHERE B.part = A.part AND B.Description <= A.Description) AS seqno
FROM descr A
ORDER BY A.part, A.Description;

Which will produce these results:

partcodeDescriptionseqno
D1001FABFeature 1 for D10011
D1001FABFeature 2 for D10012
D1002FABFeature 1 for D10021
Revised: 2011-10-20