I have a table with multiple entries and I have ordered it according to a sales criterion. So, if the entries are like:
Item Sales a 10 b 10 c 9 d 8 e 8 f 7
I want to extract the items with the highest and second highest number of sales. As such,
I would want to extract a, b and c.
Is there any function in PostgreSQL that can help with this?
To include all rows with one of the top two sales values, you could use the
dense_rank()window function:You need PostgreSQL 8.4 or later for that.
For older versions, you could: