문제

이 쿼리를 실행하려고하지만 "ora-00904 :"z1 "."longitude ": 유효하지 않은 식별자"를 얻습니다.

이 서브 쿼리의 해당 열에 액세스 할 수 있도록 이것을 다시 작성하는 방법이 있습니까? 아니면 일반적으로 내가하려는 일을 성취하는 더 좋은 방법이 있습니까?

감사

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)

From Clause의 일부로 Zip_Coords Z1을 포함시키고 WHERE에 조인을 포함시킵니다. 그런 다음 하위 쿼리에서 Z1에 액세스 할 수 있어야합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top