mysql - Result grouped by resolution with minimum distance from coordinate -
i have query find nearest latitude/longitude given coordinate :
public function findclosestbylatitudelongitude($latitude, $longitude, $distanceunit = 111.045, $radius = 150)
$stmt = $this->db->prepare('select f.fcst_latitude, f.fcst_longitude, f.fcst_resolution, :distance_unit * degrees( acos( cos( radians(:latitude) ) * cos( radians(f.fcst_latitude) ) * cos( radians(:longitude) - radians(f.fcst_longitude) ) + sin( radians(:latitude) ) * sin( radians(f.fcst_latitude) ) ) ) distance t_fcst_data_coord f f.fcst_latitude between :latitude - (:radius / :distance_unit) , :latitude + (:radius / :distance_unit) , f.fcst_longitude between :longitude - ( :radius / ( :distance_unit * cos( radians(:latitude) ) ) ) , :longitude + ( :radius / ( :distance_unit * cos( radians(:latitude) ) ) ) order distance asc limit 100 ');
the result array ordered distance, , containing resolution of forecast, :
( [0] => array ( [fcst_latitude] => 46.295396 [fcst_longitude] => 6.854558 [fcst_resolution] => 9.0 [distance] => 1.2113482186062683 ) [1] => array ( [fcst_latitude] => 46.313622 [fcst_longitude] => 6.843681 [fcst_resolution] => 3.0 [distance] => 1.4198633375521186 ) [2] => array ( [fcst_latitude] => 46.314401 [fcst_longitude] => 6.884638 [fcst_resolution] => 3.0 [distance] => 2.213273758077741 ) [3] => array ( [fcst_latitude] => 46.285180 [fcst_longitude] => 6.844827 [fcst_resolution] => 3.0 [distance] => 2.5347004607874783 ) [...] => array ( [fcst_latitude] => ... [fcst_longitude] => ... [fcst_resolution] => ... [distance] => ... ) [53] => array ( [fcst_latitude] => 46.199091 [fcst_longitude] => 6.886765 [fcst_resolution] => 27.0 [distance] => 12.064028782357124 ) [...] => array ( [fcst_latitude] => ... [fcst_longitude] => ... [fcst_resolution] => ... [distance] => ... ) )
how can have result shows unique resolution minimum distance order resolution ? expected result :
( [0] => array ( [fcst_latitude] => 46.199091 [fcst_longitude] => 6.886765 [fcst_resolution] => 27.0 [distance] => 12.064028782357124 ) [1] => array ( [fcst_latitude] => 46.295396 [fcst_longitude] => 6.854558 [fcst_resolution] => 9.0 [distance] => 1.2113482186062683 ) [2] => array ( [fcst_latitude] => 46.313622 [fcst_longitude] => 6.843681 [fcst_resolution] => 3.0 [distance] => 1.4198633375521186 ) )
i tried group fcst_resolution , select min distance, result array wrong latitude , longitudes :
( [0] => array ( [fcst_latitude] => 44.972113 [fcst_longitude] => 8.737022 [fcst_resolution] => 9.0 [distance] => 1.2113482186062683 ) [1] => array ( [fcst_latitude] => 45.231748 [fcst_longitude] => 5.680505 [fcst_resolution] => 3.0 [distance] => 1.4198633375521186 ) [2] => array ( [fcst_latitude] => 45.118703 [fcst_longitude] => 8.640296 [fcst_resolution] => 27.0 [distance] => 12.064028782357124 ) )
thanks
couple of ways it. normal way use sub query min distance each resolution, , join against query full rows each resolution / distance.
another trick group resolution, , use group_concat each of other fields, ordering distance. use substring_index chop off first occurance of each field result of group_concats. possible issues if contains null, or if field returns value containing comma.
$stmt = $this->db->prepare(' select substring_index(group_concat(sub0.fcst_latitude order sub0.distance ), ',', 1) fcst_latitude, substring_index(group_concat(sub0.fcst_longitude order sub0.distance ), ',', 1) fcst_longitude, sub0.fcst_resolution, substring_index(group_concat(sub0.distance order sub0.distance ), ',', 1) distance, (select f.fcst_latitude, f.fcst_longitude, f.fcst_resolution, :distance_unit * degrees( acos( cos( radians(:latitude) ) * cos( radians(f.fcst_latitude) ) * cos( radians(:longitude) - radians(f.fcst_longitude) ) + sin( radians(:latitude) ) * sin( radians(f.fcst_latitude) ) ) ) distance t_fcst_data_coord f f.fcst_latitude between :latitude - (:radius / :distance_unit) , :latitude + (:radius / :distance_unit) , f.fcst_longitude between :longitude - ( :radius / ( :distance_unit * cos( radians(:latitude) ) ) ) , :longitude + ( :radius / ( :distance_unit * cos( radians(:latitude) ) ) ) order distance asc limit 100 ) sub0 group sub0.fcst_resolution ');
Comments
Post a Comment