SELECT a.cust_id, a.hour_in1, a.min_in1, b.hour_in2, b.min_in2
FROM table1 a, table2 b
WHERE a.cust_id = b.cust_id
AND a.hour_in1 = b.hour_in2
AND (
a.min_in1 = b.min_in2
OR b.min_in2 = a.min_in1 +3
OR b.min_in2 = a.min_in1 +2
OR b.min_in2 = a.min_in1 +1
)
SELECT a.cust_id, a.hour_in1, a.min_in1, b.hour_in2, b.min_in2
FROM table1 a, table2 b
WHERE a.cust_id = b.cust_id
AND a.hour_in1 = b.hour_in2
AND (
a.min_in1 = b.min_in2
OR b.min_in2 = a.min_in1 +3
OR b.min_in2 = a.min_in1 +2
OR b.min_in2 = a.min_in1 +1
)
group by cust_id
order by cust_id,(min_in1-min_in2)
SELECT a.cust_id, a.hour_in1, a.min_in1, b.hour_in2, b.min_in2, min(min_in1-min_in2)
FROM t1 a, t2 b
WHERE a.cust_id = b.cust_id
AND a.hour_in1 = b.hour_in2
AND (a.min_in1- b.min_in2) <=3
group by cust_id
order by cust_id