Suppose, we have query like this:
SELECT
tt.id first_column,
tt.value second_column,
--another values
qq.code_1 third_column,
qq.code_2 column4,
qq.code_3 column5,
qq.code_4 column6,
qq.code_5 column7
FROM
test_tbl tt LEFT JOIN
(SELECT * FROM (
SELECT id, code_value, ROW_NUMBER() OVER (ORDER BY code_value) AS RN
FROM attributes attr WHERE attr.id = tt.id AND
attr.code IN ('ATTRIBUTE_CODE_1',
'ATTRIBUTE_CODE_2',
'ATTRIBUTE_CODE_3',
'ATTRIBUTE_CODE_4',
'ATTRIBUTE_CODE_5')
)
PIVOT (MAX(code_value) FOR (RN) IN (1 as code_1, 2 as code_2, 3 as code_3, 4 as code_4, 5 as code_5))) qq
ON tt.id = qq.id
-- LEFT JOINS with another tables...
The problem is error:
ORA-00904: "TT"."ID": invalid identifier
How can I circumvent this obstacle?
Of course above example is easy and dummy, real query is more complicated.
This example can be checked here: http://sqlfiddle.com/#!4/eec83/3