Skip to content
Advertisement

Dense Rank grouping by IDs

I am having trouble getting my DENSE_RANK() function in Oracle to work how I would like. First, my dataset:

I am trying to use the dense rank function to return results with a sequence number based on the DATE field, and grouping by ID. How I want the data to return:

The query I have so far:

However, this returns incorrectly as the sequence number will go to 6 (Like its disregarding my intentions to sequence based on the DATE field within a certain ID). If anyone has any insights into how to make this work it would be very much appreciated!

Advertisement

Answer

You want row_number():

You could actually use dense_rank() as well, if you want duplicates to have the same idea. The key idea is partition by.

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