tags:

views:

20

answers:

1

Hi everybody.

So I have 2 tables :

route(#id)
step(#id,route_id,lat,lng,sequence_order)

And 2 GPS coordinates : M1(lat1, lng1) M2(lat2, lng2).

So I want to find all the routes which have at least one step closer than 1.0 mile from each of the 2 GPS coordinates with a ascending sequence order.

So if I find Route#25 which has a step S1 at 0.4 miles from M1, and a step S2 at 0.2 miles from M2, it should match only if the sequence number of S1 is inferior to the sequence number of S2. Does it make sense ?

Assuming I added a function to calculate the distance, I can do it quite easily with this request :

SELECT r.id,s1.lat,s1.lng,s2.lat,s2.lng,distance(s1.lat,s1.lng,lat1,lng1) as d1,distance(s2.lat,s2.lng,lat2,lng2) as d2 FROM route r
INNER JOIN step s1 ON r.id = s1.route_id
INNER JOIN step s2 ON r.id = s2.route_id AND s1.sequence_order < s2.sequence_order
WHERE d1<1.0
AND d2<1.0 

The behavior I would like to have, is to keep only the closest coordinates (s1.lat,s1.lng) and (s2.lat,s2.lng) to my to points. So I thought I could do something like this :

ORDER BY d1
ORDER BY d2
GROUP BY r.id

but it just crashes.

Any idea ?

+1  A: 

You probably want

ORDER BY d1, d2

as your order by clause. What's the crash, anyway?

Brian Hooper
It's an assertion failure. Actually ORDER BY d1, d2 makes a secondary sorting. What I want is the closest point from the first point and the closest point from the second.
Julien
Assertion failure: I don't know anything about those, I must admit. You can expand on the ORDER BY clause and have ORDER BY d1 + d2; or ORDER BY myfunc (d1, d2); if that's any help. What you can't have is two ORDER BY clauses.
Brian Hooper
Sorry it's not assertion failure. That's the C error...Actually I'm kind of wanting to have the min of d1 and d2.I am trying a different approach, so i'll close the question. Thanks
Julien