I have a log table with the following format (and sample data):
date | time
2010/01/02 | 10:00
2010/01/02 | 13:00
2010/01/04 | 02:34
I want to make an SQL query that will retrieve results with the following format:
date1 | time1 | date2 | time2
2010/01/02 | 10:00 | 2010/01/02 | 13:00
2010/01/02 | 13:00 | 2010/01/04 | 02:34
2010/01/04 | 02:34 | <null> | <null>
So I was thinking a left outer join would do the trick:
SELECT
i.date as date1,
i.time as time1,
j.date as date2,
j.time as time2
FROM
log_table i
LEFT OUTER JOIN
log_table j
ON
i.id = j.id
AND (j.date > i.date
OR (j.date == i.date AND j.time > i.time))
However, this would result in the following:
date1 | time1 | date2 | time2
2010/01/02 | 10:00 | 2010/01/02 | 13:00
2010/01/02 | 10:00 | 2010/01/04 | 02:34
2010/01/02 | 13:00 | 2010/01/04 | 02:34
2010/01/04 | 02:34 | <null> | <null>
The database used is PostgreSql by the way.
Thanks.