Desired outcome: I want to get the 2nd earliest date for each group.
Current: I am getting the earliest date for each group
Query:
select * from
(select * from profile_match
where match_available_on is not null
order by profile_id asc, match_available_on asc)
as P1
group by P1.profile_id
Example: Link
From the example, I want to get:
- 1, '2015-05-20 03:50:11'
- 2, '2015-05-16 03:50:09'
And I hope that the query is not hard-coded with specific profile_id (1 or 2) as my actual data got lots of profile_id. Thanks guys!
This query will give you the results you want:
By finding the min
match_available_on
, we can then range join against that, and choose the minimum joined value. This isn't going to be amazingly performant, but it will get the job done.updated your demo here.
It will not return a row for a profile which has only one date available, as there is no 'second earliest'.