Query for Haversine formula in Oracle

南楼画角 提交于 2019-12-14 04:24:03

问题


This query is working fine but I want to use the MIN function in this query, because I want only one station which has the least distance from the given current location.

How can I use MIN in this query?

select *
from
(
  SELECT fc_station_mst.station_name,fc_station_mst.station_id,fc_station_mst.lat, fc_station_mst.longitude,fc_station_mst.state,
          fc_station_param_dtl.fc_date,fc_station_param_dtl.param_value,fc_parameter_mst.param_name,fc_parameter_mst.param_unit, 

  6371*(2*atan2 (sqrt(sin(((3.14*(fc_station_mst.lat-23.07))/180)/2) * 
  sin(((3.14*(fc_station_mst.lat-23.07))/180)/2)+cos((3.14*fc_station_mst.lat)/180)*cos((3.14*23.07)/180) 
  * sin(((3.14*(fc_station_mst.longitude-72.71))/180)/2)* 
  sin(((3.14*(fc_station_mst.longitude-72.71))/180)/2)),(sqrt(1-(sin(((3.14*(fc_station_mst.lat-23.07))/180)/2)
  *sin(((3.14*(fc_station_mst.lat-23.07))/180)/2) +cos((3.14*fc_station_mst.lat)/180)* cos((3.14*23.07)/180)*
  sin(((3.14*(fc_station_mst.longitude-72.71))/180)/2)* sin(((3.14*(fc_station_mst.longitude-72.71))/180)/2)))))) as distance 

  FROM fc_station_mst, fc_station_param_dtl, fc_parameter_mst 

  where fc_station_param_dtl.param_id=fc_parameter_mst.param_id and
  fc_station_mst.station_id=fc_station_param_dtl.station_id  and  fc_station_param_dtl.param_id in(1,3,5,9)  and 
  fc_station_param_dtl.hourly_forecast='24' and fc_station_param_dtl.fc_date='22-JUL-2014'
) x

where   distance <= 15 ;

回答1:


At first you have to calculate all the distances you have, then you get the min distance and compare it value with all the distances. The station that has the exact value is your answer. Like this:

with t as (
    select * 
    from
    (
      SELECT 6371*(2*atan2 (sqrt(sin(((3.14*(fc_station_mst.lat-23.07))/180)/2) * 
              sin(((3.14*(fc_station_mst.lat-23.07))/180)/2)+cos((3.14*fc_station_mst.lat)/180)*cos((3.14*23.07)/180) 
              * sin(((3.14*(fc_station_mst.longitude-72.71))/180)/2)* 
              sin(((3.14*(fc_station_mst.longitude-72.71))/180)/2)),(sqrt(1-(sin(((3.14*(fc_station_mst.lat-23.07))/180)/2)
              *sin(((3.14*(fc_station_mst.lat-23.07))/180)/2) +cos((3.14*fc_station_mst.lat)/180)* cos((3.14*23.07)/180)*
              sin(((3.14*(fc_station_mst.longitude-72.71))/180)/2)* sin(((3.14*(fc_station_mst.longitude-72.71))/180)/2)))))) as distance 
      FROM fc_station_mst, fc_station_param_dtl, fc_parameter_mst 

      where fc_station_param_dtl.param_id=fc_parameter_mst.param_id and
      fc_station_mst.station_id=fc_station_param_dtl.station_id  and  fc_station_param_dtl.param_id in(1,3,5,9)  and 
      fc_station_param_dtl.hourly_forecast='24' and fc_station_param_dtl.fc_date='22-JUL-2014'
    ) 
)
select fc_station_mst.station_name,fc_station_mst.station_id,fc_station_mst.lat, fc_station_mst.longitude,fc_station_mst.state,
       fc_station_param_dtl.fc_date,fc_station_param_dtl.param_value,fc_parameter_mst.param_name,fc_parameter_mst.param_unit
  from fc_station_mst, fc_station_param_dtl, fc_parameter_mst
     , (
        select min(distance) distance
          from t
       ) t1
 where t1.distance = 6371*(2*atan2 (sqrt(sin(((3.14*(fc_station_mst.lat-23.07))/180)/2) * 
      sin(((3.14*(fc_station_mst.lat-23.07))/180)/2)+cos((3.14*fc_station_mst.lat)/180)*cos((3.14*23.07)/180) 
      * sin(((3.14*(fc_station_mst.longitude-72.71))/180)/2)* 
      sin(((3.14*(fc_station_mst.longitude-72.71))/180)/2)),(sqrt(1-(sin(((3.14*(fc_station_mst.lat-23.07))/180)/2)
      *sin(((3.14*(fc_station_mst.lat-23.07))/180)/2) +cos((3.14*fc_station_mst.lat)/180)* cos((3.14*23.07)/180)*
      sin(((3.14*(fc_station_mst.longitude-72.71))/180)/2)* sin(((3.14*(fc_station_mst.longitude-72.71))/180)/2))))))


来源:https://stackoverflow.com/questions/25376058/query-for-haversine-formula-in-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!