I have a table as follow:
create table x(id int, work date);
this table has the following records:
insert into x values(1,'2017-01-01'),(2,'2017-01-01'),(3,'2017-02-02'),(4,'2017-01-15'),(5,'2017-01-15');
Now, I want to select the records with first, second and third date values.
My desired output would be as follows:
If I want to select record with first date value then the output would be:
id work
1 2017-01-01
If I select record with second date value then the output would be:
id work
4 2017-01-15
Any help is appreciated in advanced.
Best Answer
Use a window function:
If you need to deal with duplicate dates, use
dense_rank()
instead ofrow_number()
and use a second window function to provided numbers for each date:Online example: http://rextester.com/VQVTB53094