Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Invalid identifier oracle sql

This error seems to appear a lot and I’ve had mixed answers such as changing names, but I couldn’t see any issues with my query regarding this. Perhaps I have not spotted something?

For example:

select
  sport,
  sum(turn_over) as turnover,
  count(distinct(user_no)) as users,
  sum(turn_over) / count(distinct(user_no)) as average,
  extract(
    month
    from
      ref_date
  ) as outer_month,
  extract(
    year
    from
      ref_date
  ) as outer_year
from
  dw_unica.T_DAILY_SB
group by
  sport,
  outer_month,
  outer_year;

Gives the following error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ORA-00904: "OUTER_YEAR": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:
Error at Line: 61 Column: 271

>Solution :

You cannot use aliases defined in the SELECT clause in the GROUP BY clause as the SELECT clause is processed after the GROUP BY clause.

You need to GROUP BY the underlying function calls rather than the aliases:

select sport,
       sum(turn_over) as turnover,
       count(distinct user_no) as users,
       sum(turn_over) / count(distinct user_no) as average,
       extract(month from ref_date) as outer_month,
       extract(year from ref_date) as outer_year
from   dw_unica.T_DAILY_SB
group by
       sport,
       extract(month from ref_date),
       extract(year from ref_date);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading