Skip to content
Advertisement

Create a descending list of orders per item and display the ranking position in seperate column

DB-Fiddle

Expected Result:


In the results above I want to create a descending list of all orderIDs per itemID.
Starting from the newest order to the oldest order which is defined by the event_date.
The position of an orderID within a certain itemID should be displayed in column position.


I tried to go with this query but could not make it work:

What do I need to change to get the expected result?

Advertisement

Answer

You must partition by itemid and order by MAX(event_date) DESC in the ROW_NUMBER() window function:

See the demo.
Results:

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