Why do I get Invalid identifier error even though column exist
Code Review


line 3-6


Columns are not fully qualified

line 2 &amp; 21-25


DISTINCT and GROUP BY doesn't make sense in this context.  drop one

line 26-28


I wouldn't trust an ORDER BY clause in a (refactored) sub-query. Move it to the main query or drop it.

line 35-41


It's 2019.  Please use ANSI JOINs

line 34


There are duplicate column names in a SELECT statement.  Give one of them an alias.



Most likely, your problem will be fixed by (5)