sql - Finding the min distance MYSQL -
i want find min distance between (x2-x1)^2 + (y2-y1)^2 particular macid , timestamp. i'm trying find nearest possible gate location individual @ 1 particular instance of time. so, query should return 1 unique value of user @ 1 instance of time min gate location.
the data set looks like:
x1 y1 timestamp macid x2 y2 gate | 5618 | 5303 |1 12:22:02 | 54:ea:a8:53:5b:eb | 5844 | 5377 | c24 | 5848 | 5046 |1 12:22:02 | 54:ea:a8:53:5b:eb | 5844 | 5377 | c18 | 6094 | 5464 |1 12:22:02 | 54:ea:a8:53:5b:eb | 5844 | 5377 | c17 | 6021 | 6540 |1 13:09:48 | 48:5a:3f:6a:01:b9 | 6210 | 6801 | c23 | 6366 | 7036 |1 13:09:48 | 48:5a:3f:6a:01:b9 | 6210 | 6801 | c14 | 6366 | 7036 |1 13:09:48 | 48:5a:3f:6a:01:b9 | 6210 | 6801 | c13
the result set should below:
x1 y1 timestamp macid x2 y2 gate | 5848 | 5046 |1 12:22:02 | 54:ea:a8:53:5b:eb | 5844 | 5377 | c18 | 6021 | 6540 |1 13:09:48 | 48:5a:3f:6a:01:b9 | 6210 | 6801 | c23
i have tried below query not working:
select min((x2-x1)^2 + (y2-y1)^2), macid, timestamp maptable groupbymacid, timestamp
i tried using self joins seems wrong.
may know i'm going wrong.
you use query:
select m.* maptable m, ( select timestamp, macid, min(pow((x2-x1), 2) + pow((y2-y1), 2)) mindist maptable group timestamp, macid ) m.timestamp = a.timestamp , m.macid = a.macid , pow((x2-x1), 2) + pow((y2-y1), 2) = a.mindist;
sql fiddle: http://sqlfiddle.com/#!9/d7979/6
but note not return 1 row per macid , date, because in input data 2 final rows same, min distance same gates c13 , c14
Comments
Post a Comment