0

My query is below, but for some reason PostgreSQL is not recognizing the row. Let me know what is wrong here. I am using PgAdmin 4 also.

QUERY:

SELECT date_part('year',end_of_quarter) as yr,industry,sum(gdp) as totalgdp FROM 
(SELECT * FROM income where industry IN ('Mining','Construction','Utilities')) as MCU
GROUP BY yr,industry
HAVING yr = 2013
ORDER BY totalgdp desc;

RESULT:

ERROR:  column "yr" does not exist
LINE 4: having yr = 2013
               ^
SQL state: 42703
Character: 196
S-Man
  • 22,521
  • 7
  • 40
  • 63
SantiClaus
  • 666
  • 2
  • 8
  • 22

1 Answers1

4

HAVING cannot use the alias of an expression. You can HAVING date_part('year', end_of_quarter) = 2013 instead.

Catalyst
  • 3,143
  • 16
  • 20