Oracle sql - Invalid Identifier LISTAGG
<p>I'm new using the <code>LISTAGG</code> function. When I run the following script I get an invalid identifier error:</p>

<blockquote>
  <p>ORA-00904: "TE"."COUNTRY": invalid identifier</p>
</blockquote>

<p>Any ideas why?</p>

<pre><code>SELECT te.country, listagg(te.exception_date, ' ,') WITHIN GROUP (ORDER BY te.country) country
FROM 
(select unique te.exception_date, 
case when te.country is null then (select max(tt.country) from tt_transport tt where te.route = tt.route) else te.country end country
  from tt_exception te
  where trunc(te.exception_date) &gt; '01-JAN-2015'
  and te.plant = 'Z'
  and not case when te.country is null then (select max(tt.country) from tt_transport tt where te.route = tt.route) else te.country end is null
  order by te.country) te
group by te.country
UNION ALL
SELECT te.country, listagg(te.exception_date, ' ,') WITHIN GROUP (ORDER BY te.country) country
FROM 
(select unique te.exception_date, 'GB' country
  from tt_exception te
  where trunc(te.exception_date) &gt; '01-JAN-2015'
  and te.plant = 'W'
  and te.country is null
  order by te.country)
group by te.country
</code></pre>