Happydevdays
Happydevdays

Reputation: 2082

how to select records that are "similar" in value

Let's say I have table1 with a column like this:

         minutes     | numeric(5,1)             | 

and table2 with the same column:

         minutes     | numeric(5,1)             | 

How would I select records from table1 that are within a few minutes difference of values in table2? Let's say table1 has a value of 5 minutes... I want to look in table2 for records that are either 1 minute more or 1 minute less.

My data in table 1 looks like this:

minutes

 9.9
 0.5
 0.2
 0.4
 1.1

so for the first record, I would be looking to match records in table2 that are between 8.9 and 10.9

I'm trying to find the correct key words to google but I haven't been successful.

Upvotes: 0

Views: 44

Answers (1)

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49270

Take the absolute value of the difference and check to see if it is less than or equal to the desired number.

select t1.*
from table1 t1
join table2 t2 on abs(t1.minutes-t2.minutes) <= 1

Upvotes: 2

Related Questions