Search code examples
sqlsql-servert-sqlrow-numberrownum

I want to generate continuously number by 2 column and batch wise


I want to generate continuously number with the combination of 2 columns and in batch size of 5. Anybody can help to solve this?

This is my input and desired output is given:


Solution

  • An adoption of @GordonLinoff's answer...

    SELECT
        name,
        rank, 
        DENSE_RANK() OVER (ORDER BY name DESC, Rank, ((seqnum - 1) / 5))  AS rno   
    FROM
    (
        SELECT
            *,
            ROW_NUMBER() OVER (PARTITION BY name, rank ORDER BY (SELECT null))   AS seqnum
        FROM
            yourTable
    )
        sequenced
    ORDER BY
        3