Ranking For each group

  • Hi All,
    I have the following table:
    declare @T table
    (
    ID int identity
    , NUM int
    );
    insert into @T values
    (0),(0),(0),(1),(1),(0),(0),(0),(1),(1),(0),(0),(0);

    ID NUM
    1 0
    2 0
    3 0
    4 1
    5 1
    6 0
    7 0
    8 0
    9 1
    10 1
    11 0
    12 0
    13 0
    I would like to rank each group in NUM column,like this
    ID NUM Rank
    1 0 1
    2 0 1
    3 0 1
    4 1 2
    5 1 2
    6 0 3
    7 0 3
    8 0 3
    9 1 4
    10 1 4
    11 0 5
    12 0 5 
    13 0 5

    I tried to use DENSE_RANK(),RANK() but no luck.Any Ideas?
    BTW i posted on T_SQl 2008 forum as well

    Thank you advance

  • https://www.sqlservercentral.com/Forums/1889627/Ranking-for-each-group is the duplicate. I'd post any replies there instead of here.

    Cheers!

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply