Tables :-
Table 1 :- Person_LatLong
Person_Id
Lat
Lng
Location_DateTime
Person_LatLong Data :-
10  23.030211421184454  72.55705489668806   2014-09-02 04:23:42
10  23.03029215373424   72.55710855670746   2014-09-02 04:25:09
10  23.030301667271765  72.55715279051745   2014-09-02 04:27:21
11  19.045563510415214  72.915665750359 2014-09-02 08:22:07
11  19.046567530190785  72.91524330185979   2014-09-02 08:32:36
11  19.04553070382594   72.91621148133476   2014-09-02 08:39:47
11  18.537267778519347  73.83538450865574   2014-09-03 01:44:19
11  18.33554237666039   73.85274219500492   2014-09-03 07:18:02
11  18.331919816746026  73.8525499279805    2014-09-03 07:18:59
11  18.33181875247372   73.85243149060277   2014-09-03 07:19:02
11  18.777939290860722  73.31834934181029   2014-09-04 22:07:31
11  18.790032969638293  73.30265963437363   2014-09-04 22:09:06
11  18.79108238318203   73.29928216416553   2014-09-04 22:09:22
11  18.800857529132163  73.28531940244517   2014-09-04 22:11:22
11  18.812675453346255  73.27794458217039   2014-09-04 22:13:26
11  18.82985965773455   73.25592224937081   2014-09-04 22:15:31
11  18.84531169311457   73.23344887176076   2014-09-04 22:17:49
11  18.869063931831764  73.2185512231118    2014-09-04 22:19:54
11  18.893204517796047  73.20479873759578   2014-09-04 22:22:03
11  18.910161939581506  73.18348844819505   2014-09-04 22:24:30
Table 2:- LS_For
Subject_Id -> This is basically Person_Id but I have changed the name.
Watcher_Id
Assistance_Group_Id
LS_For Table Data :-
1   10  1
1   11  1
1   17  1
Query:-
SELECT *,( 6371 * acos( cos( radians(23.030) ) 
* cos( radians(Lat) ) * cos( radians(Lng) - radians(72.5570) )
 + sin( radians(23.030) ) * sin( radians(Lat) ) ) ) 
AS distance FROM Person_LatLong WHERE Person_Id IN 
(SELECT Watcher_Id FROM LS_For WHERE Subject_Id = 1 AND Assistance_Group_Id = 1)
HAVING distance < 5 ORDER BY Location_DateTime DESC;
Gives Below as Output :- ID | LAT | LNG | DATE TIME | DISTANCE
10  23.030301667271765  72.55715279051745   2014-09-02 04:27:21 0.037008818510632306
10  23.03029215373424   72.55710855670746   2014-09-02 04:25:09 0.03433299317128307
10  23.030211421184454  72.55705489668806   2014-09-02 04:23:42 0.02417068347133403
11  23.030301667271765  72.55715279051745   2014-09-02 05:27:21 0.037008818510632306
11  23.03029215373424   72.55710855670746   2014-09-02 05:25:09 0.03433299317128307
11  23.030211421184454  72.55705489668806   2014-09-02 05:23:42 0.02417068347133403
After Adding GROUP BY :-
SELECT *,( 6371 * acos( cos( radians(23.030) ) 
* cos( radians(Lat) ) * cos( radians(Lng) - radians(72.5570) )
 + sin( radians(23.030) ) * sin( radians(Lat) ) ) ) 
AS distance FROM Person_LatLong WHERE Person_Id IN 
(SELECT Watcher_Id FROM LS_For WHERE Subject_Id = 1 AND Assistance_Group_Id = 1)
GROUP BY Person_Id HAVING distance < 5 ORDER BY Location_DateTime DESC;
Gives me below Output :-
10  23.030211421184454  72.55705489668806   2014-09-02 04:23:42 0.02417068347133403
But I want to get latest time row not the oldest.
OUTPUT Required :- Working with 2 tables, LS_For is table where I am finding the Ids for which I have to look for the location distance and based on that need to filter the result based on distance.
10  23.030301667271765  72.55715279051745   2014-09-02 04:27:21 0.037008818510632306
11  23.030301667271765  72.55715279051745   2014-09-02 05:27:21 0.037008818510632306
 
     
     
     
     
    