I have a postgresql table storing posts from an online forum. Each post belongs to a thread. I want to calculate the time it takes for the post that starts a thread to get its first response (sometimes a thread never gets a response, so that has to be taken in to consideration)
The posts table has these fields:
post_id, post_timestamp, thread_id
There can be one or more posts per thread_id. This query, for example, returns the first and second post of a thread with id 1234:
select * from posts where thread_id = 1234 order by post_timestamp limit 2
I want to calculate the time difference between first and second post and store it in a separate table with these fields:
thread_id, seconds_between_1s_and_2nd
, or, in
PostgreSQL 8.4+:To express this in seconds, use
EXTRACT(epoch FROM AGE(time1, time2))