Skip to content
Advertisement

How to use DENSE_RANK and order by aditional column?

I am trying to use dense_rank to get element order for instance:

I have table seq_test with data:

after I run the code above, I’ve got :

I want to achieve:

So DENSE_RANK() function use its own order defined in function definition I need to order the SEQ column by sequence column.

Update:

I want to get:

Advertisement

Answer

Since the conventional ORDER BY clause is performed after the analytic processing. So the ORDER BY clause of the SELECT statement will always take precedence over that comes from the order of the rows as they are processed by an analytic function.

In your case ORDER BY sequence overrides ORDER BY role_name which comes from the analytic function .

Btw, what you need depending on the last comment might be resolved by adding an extra MIN() analytic function such as

Demo

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