0

I want to select average salary by department number and age, I used this query

SELECT 
    DEPARTMENT_NUMBER,
    ROUND((MONTHS_BETWEEN(SYSDATE, BIRTHDAY)) / 12) AS AGE,
    AVG(SALARY)
FROM 
    EMPLOYEE 
GROUP BY 
    DEPARTMENT_NUMBER, AGE;

but I am getting error:

ORA-00904: "AGE": invalid identifier

1
  • 1
    Tagged Oracle . . . Oracle errors come from the Oracle database. Commented Sep 2, 2014 at 17:37

2 Answers 2

5

You cannot use a column alias in the group by in Oracle. Just use a subquery:

SELECT DEPARTMENT_NUMBER, AGE, AVG(SALARY)
FROM (SELECT e.*, ROUND((MONTHS_BETWEEN(SYSDATE,BIRTHDAY))/12) AS AGE
      FROM EMPLOYEE
     ) e
GROUP BY DEPARTMENT_NUMBER, AGE;
Sign up to request clarification or add additional context in comments.

Comments

4

You cannot group by an alias, try this:

    SELECT DEPARTMENT_NUMBER
           ,ROUND((MONTHS_BETWEEN(SYSDATE,BIRTHDAY))/12) AS AGE
           ,AVG(SALARY)
    FROM EMPLOYEE 
    GROUP BY DEPARTMENT_NUMBER, ROUND((MONTHS_BETWEEN(SYSDATE,BIRTHDAY))/12);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.