• dwain.c (8/28/2012)


    Look at the RANK() and DENSE_RANK() window functions.

    Not sure how RANK() OR DENSE_RANK() would help in this example. I thought a lot about the functions RANK, DENSE_RANK, and ROW_NUMBER and am hoping someone can see something clever that won't necessitate joining on the result of these functions...

    These functions applied to the "col1" data I gave above would give:

    col1rankdenserow_num

    5111

    4222

    3333

    2444

    1555

    1556

    1557

    So basically, I would like something that says something like:

    select where rank <= 5 and the max row_num associated with that rank is also <= 5

    Is there a way to set the assignment of the rank function? So that instead of setting all the tied values to the next integer, it would set it to the maximum row number for that value?

    My ideal MYRANK function would give

    col1myrank

    51

    42

    33

    24

    17

    17

    17

    Then I could just SELECT col1 WHERE myrank <= 5.

    I feel like there's got to be a simple explanation that I'm just not seeing... all help is greatly appreciated!