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?
decodeis not a valid builtin function AFAIK, and that's not valid syntax for calling a UDF named "decode". So what's going on there?decode(name, 'longitude', stringvalue, NULL)is (approximately?) equivalent toCASE WHEN name = 'longitude' THEN stringvalue END.