В PostgreSQL 8.4
:
SELECT date AS date1, time AS time1,
LEAD(date) OVER (ORDER BY date, time, id) AS date2,
LEAD(time) OVER (ORDER BY date, time, id) AS time2
FROM log_table
ORDER BY
date, time, id
, или просто так:
SELECT date1, time1, (lnext).*
FROM (
SELECT date AS date1, time AS time1,
LEAD(lt) OVER (ORDER BY date, time, id) AS lnext
FROM log_table lt
) q
ORDER BY
date, time, id
В PostgreSQL 8.3
и ниже:
SELECT date AS date1, time AS time1,
(li).date AS date2, (li).time AS time2
FROM (
SELECT lo.*,
(
SELECT li
FROM log_table li
WHERE (li.date, li.time, li.id) > (lo.date, lo.time, lo.id)
ORDER BY
date, time, id
LIMIT 1
) AS li
FROM log_table lo
) q