I am writing a sql query in Oracle that’s something like this:
SELECT *
FROM ( SELECT testid,
max(decode(name, 'longitude', stringvalue, NULL)) as longitude,
max(decode(name, 'latitude', stringvalue, NULL)) as latitude
FROM test_av
GROUP BY testid
) av
INNER JOIN (
SELECT id,
((ACOS(
SIN(16.15074 * 3.141592653 / 180)
* SIN(latitude * 3.141592653 / 180)
+ COS(16.15074 * 3.141592653 / 180)
* COS(latitude * 3.141592653 / 180)
* COS((-22.74426 - longitude)*3.141592653 / 180)
)*6373)) as distance
FROM test
) t ON t.id = av.testid
WHERE t.distance <= 100
When I execute this query Oracle is saying 'longitude invalid identifier'. I was trying to access sub query alias, but the query is failing.
How can I access ‘alias’ of one sub query into another sub query?
If I’m understanding correctly what you’re trying to do, you don’t actually need the
INNER JOIN, because you’re not taking any real information fromtestthat’s not already ontest_av. So, you can write:If you want to explicitly ensure that you only get records that exist in
test— that is, if you have records intest_avthat don’t have parents intest, and you want to filter those out — then you can handle that in the innermost subquery, after yourFROM test_av.