Dave
Dave

Reputation: 19150

How do I compare two TIMESTAMP columns to check for a difference of at most 15 minutes?

I'm using PostGres 9.5. I have a column in my table, article, of type TIMESTAMP. I would like to write a query in which one of the conditions is to compare two articles whose dates are separated by at most 15 minutes. I tried this ...

where extract(minute from a2.created_on - a1.created_on) < 15

but I'm realizing this is incorrect. This returns articles separted by 15 minutes but also articles separated by an hour and 15 minutes and two hours, 15 minutes, etc. How do I refine my condition so that it only considers articles separated by 15 minutes?

Upvotes: 1

Views: 442

Answers (1)

Pavel Stehule
Pavel Stehule

Reputation: 45795

It should be more simple:

WHERE a2.created_on - a1.created_on < '15min'

Difference of two timestamp values is a interval value.

Upvotes: 1

Related Questions