Oracle 半正矢查询
我目前必须检查我的查询并将它们转移到使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您采取稍微不同的方法。
查看此网站: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"
我知道如何在 SQL Server 中执行此操作,这应该很容易移植到 Oracle:
这是我创建的 UDF,用于使用半正弦公式获取两个邮政编码之间的近似乌鸦飞行距离:
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: