繁体   English   中英

Oracle SQL无效标识符

[英]Oracle SQL Invalid Identifier

我正在尝试运行此查询,但收到“ ORA-00904:“ Z1”。“ LONGITUDE”:无效的标识符“

有没有一种方法可以重写它,以便我可以访问exist子查询中的该列? 还是一般来说,有一种更好的方法可以实现我想要做的事情?

谢谢

select zip, count(UNIQUE address_id) LOCATIONS
from records 
inner join addresses a using(address_id) 
inner join zip_coords z1 using(zip)
where exists
(
  select 1 from (
    select distance(z1.latitude, z1.longitude, z2.latitude, z2.longitude) d
    from zip_coords z2
    where z2.zip in (
      select zip from available_zips
    )
  ) where d <= 50
)
GROUP BY ZIP

您的问题是您不能将这么多级别归入子查询。 我可能会漏掉一些查询内容,但可能会:

select 1 from (
    select distance(z1.latitude, z1.longitude, z2.latitude, z2.longitude) d
    from zip_coords z2
    where z2.zip in (
      select zip from available_zips
    )
  ) where d <= 50

不能改写为:

SELECT 1
FROM zip_coords z2
WHERE z2.zip IN (
  SELECT zip FROM available_zips
)  
AND distance(z1.latitude, z1.longitude, z2.latitude, z2.longitude) <= 50
select zip, count(UNIQUE address_id) LOCATIONS
from records 
inner join addresses a using(address_id) 
inner join zip_coords z1 using(zip)
where 
(
    select min(distance(z1.latitude, z1.longitude, z2.latitude, z2.longitude)) d
    from zip_coords z2
    inner join available_zips using(zip)
) <= 50
GROUP BY ZIP

我必须警告您,我不知道这将如何影响查询性能。

而不是使用:

inner join zip_coords z1 using(zip)

尝试将zip_coords z1作为FROM子句的一部分,并将联接包括在WHERE中。 然后,您应该能够从子查询访问z1。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM