Sequentially number rows by keyed group in SQL?

前端 未结 2 1177
野的像风
野的像风 2020-12-13 20:42

Is there a way in SQL to sequentially add a row number by key group?

Assume a table with arbitrary (CODE,NAME) tuples. Example table:



        
2条回答
  •  一整个雨季
    2020-12-13 21:10

    • SQL Server
    • Oracle
    • Postgres
    • Sybase

    MySQL doesn't AFAIK. This covers most bases..

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

提交回复
热议问题