Sequentially number rows by keyed group in SQL?

This covers most bases.

SELECT
    CODE,
    ROW_NUMBER() OVER (PARTITION BY CODE ORDER BY NAME) - 1 As C_NO,
    NAME
FROM
    MyTable

Leave a Comment