Oracle 半正矢查询

发布于 2024-12-04 08:08:54 字数 1459 浏览 0 评论 0原文

我目前必须检查我的查询并将它们转移到使用 Oracle 而不是 SQLSERVER,并且我对我正在使用的这个查询有点卡住了 这里

SELECT TOP 1 * FROM ( SELECT o.outcode AS lead_postcode, v.location, 
v.location_name, v.outcode AS venue_postcode, 6371.0E * 
( 2.0E *asin(case when 1.0E < (sqrt(square(sin(((RADIANS(CAST(o.lat AS FLOAT)))-
(RADIANS(CAST(v.lat AS FLOAT))))/2.0E)) + (cos(RADIANS(CAST(v.lat AS FLOAT))) 
* cos(RADIANS(CAST(o.lat AS FLOAT))) * square(sin(((RADIANS(CAST(o.lng AS FLOAT)))-
(RADIANS(CAST(v.lng AS FLOAT))))/2.0E))))) then 1.0E else 
(sqrt(square(sin(((RADIANS(CAST(o.lat AS FLOAT)))-(RADIANS(CAST(v.lat AS FLOAT))))
/2.0E)) + (cos(RADIANS(CAST(v.lat AS FLOAT))) * cos(RADIANS(CAST(o.lat AS FLOAT)))
* square(sin(((RADIANS(CAST(o.lng AS FLOAT)))-(RADIANS(CAST(v.lng AS FLOAT))))
/2.0E))))) end )) AS distance FROM venue_postcodes v, uk_postcodes o 
WHERE o.outcode = @nrpostcode ) i WHERE distance<100 ORDER BY distance

现在我知道这是一个可怕的查询,但 Oracle 似乎有很多与它有关的问题。

首先,它不喜欢 6371E 中的 E 以及所有后续的 E 其次

,它不喜欢 square< /code> 函数,所以我决定使用 power 函数,但这仍然给我带来错误。

第三,它不喜欢 radians 函数

第四,它不喜欢 TOP 1 部分,因此我将其更改为在中使用 ROWNUM WHERE 子句

我完全不知道在这里要做什么。

关于我能做些什么来让它发挥作用有什么想法吗?

提前致谢

I'm currently having to go through my queries and transfer them over to using Oracle rather than SQLSERVER and i'm a bit stuck with this query which i'm using from here

SELECT TOP 1 * FROM ( SELECT o.outcode AS lead_postcode, v.location, 
v.location_name, v.outcode AS venue_postcode, 6371.0E * 
( 2.0E *asin(case when 1.0E < (sqrt(square(sin(((RADIANS(CAST(o.lat AS FLOAT)))-
(RADIANS(CAST(v.lat AS FLOAT))))/2.0E)) + (cos(RADIANS(CAST(v.lat AS FLOAT))) 
* cos(RADIANS(CAST(o.lat AS FLOAT))) * square(sin(((RADIANS(CAST(o.lng AS FLOAT)))-
(RADIANS(CAST(v.lng AS FLOAT))))/2.0E))))) then 1.0E else 
(sqrt(square(sin(((RADIANS(CAST(o.lat AS FLOAT)))-(RADIANS(CAST(v.lat AS FLOAT))))
/2.0E)) + (cos(RADIANS(CAST(v.lat AS FLOAT))) * cos(RADIANS(CAST(o.lat AS FLOAT)))
* square(sin(((RADIANS(CAST(o.lng AS FLOAT)))-(RADIANS(CAST(v.lng AS FLOAT))))
/2.0E))))) end )) AS distance FROM venue_postcodes v, uk_postcodes o 
WHERE o.outcode = @nrpostcode ) i WHERE distance<100 ORDER BY distance

Now I know this is a horrible query to look at but Oracle seems to be having a lot of problems with it.

Firstly it doesn't like the E in 6371E and all the subsequent E's

Secondly it doesn't like the square function so I decided to use the power function but this still gave me errors.

Thirdly it doesn't like the radians function

Fourthly it doesn't like the TOP 1 part so I had changed this to use ROWNUM in the WHERE clause

I'm completely lost as to what to do here.

Any ideas as to what I can do to make it work?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日情怀 2024-12-11 08:08:54

我建议您采取稍微不同的方法。

查看此网站:http://psoug.org/reference/functions.html

查找涉及“计算距离”的部分

I'd recommend you take a slightly different approach.

Check out this site: http://psoug.org/reference/functions.html

Look for the part referring to "calc distance"

聊慰 2024-12-11 08:08:54

我知道如何在 SQL Server 中执行此操作,这应该很容易移植到 Oracle:

这是我创建的 UDF,用于使用半正弦公式获取两个邮政编码之间的近似乌鸦飞行距离:

ALTER FUNCTION [dbo].[fn_GetZipDistanceMiles](
    @ZipFrom VARCHAR(20),
    @ZipTo VARCHAR(20)
)
RETURNS FLOAT 
AS  
BEGIN 

    DECLARE @Latitude1 FLOAT
    DECLARE @Longitude1 FLOAT
    DECLARE @Latitude2 FLOAT
    DECLARE @Longitude2 FLOAT

    SELECT  @Latitude1 = Latitude,
            @Longitude1 = Longitude
    FROM    ZipCode
    WHERE   ZipCode = @ZipFrom


    SELECT  @Latitude2 = Latitude,
            @Longitude2 = Longitude
    FROM    ZipCode
    WHERE   ZipCode = @ZipTo

    -- CONSTANTS
    DECLARE @EarthRadiusInMiles FLOAT
    SET @EarthRadiusInMiles = 3963.1

    -- RADIANS conversion
    DECLARE @Lat1Radians FLOAT
    DECLARE @Long1Radians FLOAT
    DECLARE @Lat2Radians FLOAT
    DECLARE @Long2Radians FLOAT

    SET @Lat1Radians = @Latitude1 * PI() / 180
    SET @Long1Radians = @Longitude1 * PI() / 180
    SET @Lat2Radians = @Latitude2 * PI() / 180
    SET @Long2Radians = @Longitude2 * PI() / 180

    RETURN ACOS(COS(@Lat1Radians) * COS(@Long1Radians) * COS(@Lat2Radians) * COS(@Long2Radians) + COS(@Lat1Radians) * SIN(@Long1Radians) * COS(@Lat2Radians) * SIN(@Long2Radians) + SIN(@Lat1Radians) * SIN(@Lat2Radians)) * @EarthRadiusInMiles

END

I know how to do it in SQL Server, which should be easy enough to port over to Oracle:

Here's a UDF I created to get the approximate crows flight distance between two zip codes using the Haversine formula:

ALTER FUNCTION [dbo].[fn_GetZipDistanceMiles](
    @ZipFrom VARCHAR(20),
    @ZipTo VARCHAR(20)
)
RETURNS FLOAT 
AS  
BEGIN 

    DECLARE @Latitude1 FLOAT
    DECLARE @Longitude1 FLOAT
    DECLARE @Latitude2 FLOAT
    DECLARE @Longitude2 FLOAT

    SELECT  @Latitude1 = Latitude,
            @Longitude1 = Longitude
    FROM    ZipCode
    WHERE   ZipCode = @ZipFrom


    SELECT  @Latitude2 = Latitude,
            @Longitude2 = Longitude
    FROM    ZipCode
    WHERE   ZipCode = @ZipTo

    -- CONSTANTS
    DECLARE @EarthRadiusInMiles FLOAT
    SET @EarthRadiusInMiles = 3963.1

    -- RADIANS conversion
    DECLARE @Lat1Radians FLOAT
    DECLARE @Long1Radians FLOAT
    DECLARE @Lat2Radians FLOAT
    DECLARE @Long2Radians FLOAT

    SET @Lat1Radians = @Latitude1 * PI() / 180
    SET @Long1Radians = @Longitude1 * PI() / 180
    SET @Lat2Radians = @Latitude2 * PI() / 180
    SET @Long2Radians = @Longitude2 * PI() / 180

    RETURN ACOS(COS(@Lat1Radians) * COS(@Long1Radians) * COS(@Lat2Radians) * COS(@Long2Radians) + COS(@Lat1Radians) * SIN(@Long1Radians) * COS(@Lat2Radians) * SIN(@Long2Radians) + SIN(@Lat1Radians) * SIN(@Lat2Radians)) * @EarthRadiusInMiles

END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文