0

My table has an NAME and DISTANCE column. I'd like to figure out a way to list all the names that are within N units or less from the same name. i.e. Given:

NAME    DISTANCE
a   2
a   4
a   3
a   7
a   1
b   3
b   1
b   2
b   5

(let's say N = 2) I would like

a 2 
a 4 
a 3
a 1
...
...

Instead of a 2 a 2 (because it double counts)

I'm trying to apply this method in order to solve for a customerID with claim dates (stored as number) that appear in clusters around each other. I'd like to be able to label the customerID and the claim date that is within say 10 days of another claim by that same customer. i.e., |a.claimdate - b.claimdate| <= 10. When I use this method

WHERE a.CUSTID = b.CUSTID
AND a.CLDATE BETWEEN (b.CLDATE - 10 AND b.CLDATE + 10)
AND a.CLAIMID <> b.CLAIMID

I double count. CLAIMID is unique.

2
  • Are you using mysql? or sql server?
    – John Conde
    May 9, 2013 at 19:28
  • Using microsoftSQL but mysql syntax would be great
    – Rob S
    May 9, 2013 at 19:31

2 Answers 2

2

Since you don't need the text, and just want the values, you can accomplish that using DISTINCT:

select distinct t.name, t.distance
from yourtable t
  join yourtable t2 on t.name = t2.name 
    and (t.distance = t2.distance+1 or t.distance = t2.distance-1)
order by t.name

SQL Fiddle Demo

Given your edits, if you're looking for results between a certain distance, you can use >= and <= (or BETWEEN):

select distinct t.name, t.distance
from yourtable t
  join yourtable t2 on t.name = t2.name 
    and t.distance >= t2.distance-1 
    and t.distance <= t2.distance+1
    and t.distance <> t2.distance
order by t.name

You need to add the final criteria of t.distance <> t2.distance so you don't return the entire dataset -- technically every distance is between itself. This would be better if you had a primary key to add to the join, but if you don't, you could utilize ROW_NUMBER() as well to achieve the same results.

with cte as (
  select name, distance, row_number() over (partition by name order by (select null)) rn
  from yourtable
  )
select distinct t.name, t.distance
from cte t
  join cte t2 on t.name = t2.name 
    and t.distance >= t2.distance-1
    and t.distance <= t2.distance+1
    and t.rn <> t2.rn
order by t.name

Updated SQL Fiddle

9
  • Not sure if this makes much difference performance-wise, but I'd replace the or with and t.distance in (t2.distance + 1, t2.distance - 1), mostly for readability.
    – dang
    May 9, 2013 at 19:42
  • 1
    I took the question to mean that the distance N could be supplied as a input. If that's the case then there needs to be a range change, not IN, for the distance.
    – HABO
    May 9, 2013 at 19:46
  • Good point, @HABO. Took that into consideration in my own answer.
    – dang
    May 9, 2013 at 19:48
  • 1
    @RobS -- instead of using distance = distance + 1 use >= and <= respectively. So for example, if you were looking for those matches within 5 of one another, (t.distance >= t2.distance-5 AND t.distance <= t2.distance+5) -- sqlfiddle.com/#!3/8b16e/1 -- You could also use the Between operator, but I prefer >= and <=. Best of luck!
    – sgeddes
    May 9, 2013 at 20:40
  • 1
    @RobS -- btw, technically you need to add to that fiddle since you now want between distances vs exact distances (sqlfiddle.com/#!3/8b16e/7) -- the problem is it will always return the entire data set with between since every distance is between itself :) Adding t.distance <> t2.distance ensures this won't happen. However, it would be better to do this on a primary key. Best regards!
    – sgeddes
    May 9, 2013 at 20:50
1

I like @sgeddes' solution, but you can also get rid of the distinct and or in the join condition like this:

select * from table a
where exists (
    select 1 from table b
    where b.name = a.name
        and b.distance between a.distance - 1 and a.distance + 1
)

This also ensures that rows with equal distance get included and considers a whole range, not just the rows that have a distance difference of exactly n, as suggested by @HABO.

2
  • 1
    I like where you're going with this, but you'd need to also add criteria not to check for itself. Here's what I mean (sqlfiddle.com/#!3/4c89f/2). This returns the incorrect results. You can fix this by introducing ROW_NUMBER(). Or technically you can add and a.distance <> b.distance, but for different rows with the same distance, this wouldn't be a good solution. Nice usage of exists though -- +1
    – sgeddes
    May 9, 2013 at 20:57
  • Kinda hard to differentiate rows without a PK or unique column(s). I guess you could create a table variable or temp table with an identity column and then compare on top of that, including and a.id <> b.id in the subquery.
    – dang
    May 9, 2013 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.