sqloracle-databaseselectora-00904

How to use temporary column's name in WHERE clause?


First of all, I have to say that I had researched my question before came here and saw a similar question here and all its answers, but unfortunately it hasn't helped me.

Oracle SQL.

I have :

SELECT 
    first_name, 
    salary_dollars, 
    0.85 * salary_dollars AS salary_euro
FROM 
    employees 
WHERE 
    0.85 * salary_dollars > 4000;

Where the salary_euro is my temporary column. I want to avoid double computation in WHERE clause, but if i write WHERE salary_euro > 4000 in my request then I get this error:

ORA-00904: "SALARY_EURO": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 6 Column: 7


Solution

  • One way around this restriction is to use a subquery to create your temporary columns:

    SELECT * 
    FROM   (SELECT first_name, 
                   salary_dollars, 
                   0.85 * salary_dollars AS salary_euro
           FROM    employees) e
    WHERE  salary_euro > 4000;