Should be the easiest and fastest with LEFT JOINand DISTINCT ON:
WITH x(search_ts) AS (
VALUES
('2012-07-26 20:31:29'::timestamp)
,('2012-05-14 19:38:21')
,('2012-05-13 22:24:10')
)
SELECT DISTINCT ON (x.search_ts)
x.search_ts, r.id, r.resulttime
FROM x
LEFT JOIN results r ON r.resulttime <= x.search_ts
ORDER BY x.search_ts, r.resulttime DESC;
Result (dummy values):
search_ts | id | resulttime
--------------------+--------+----------------
2012-05-13 22:24:10 | 404643 | 2012-05-13 22:24:10
2012-05-14 19:38:21 | 404643 | 2012-05-13 22:24:10
2012-07-26 20:31:29 | 219822 | 2012-07-25 19:47:44
CTE, , , generate_series() - . ( generate_series() "generate_sequence()"?)
JOIN resulttime. LEFT JOIN JOIN, , resulttime.
DISTINCT ON (x.search_ts) ORDER BY x.search_ts, r.resulttime DESC ( ) resulttime, .