Skip to content
Advertisement

Ranking subcategories while keeping order of ID

I have the following table:

I would like to rank the subcategory (cat2).

Desired outcome:

I use DENSE_RANK with PARTITION BY to get the following result:

Statement:

As you can see, the only thing I’m missing is the order of the result. Currently, the rank is based on the alphabetic order of cat2. However, I want to preserve the order given by the id’s (see desired outcome). Simply changing the ORDER BY of my DENSE_RANK will not do the trick.

Advertisement

Answer

fiddle

Of course, CTE may be converted to subquery.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement