简体   繁体   中英

Oracle sql - Invalid Identifier LISTAGG

I'm new using the LISTAGG function. When I run the following script I get an invalid identifier error:

ORA-00904: "TE"."COUNTRY": invalid identifier

Any ideas why?

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) > '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) > '01-JAN-2015'
  and te.plant = 'W'
  and te.country is null
  order by te.country)
group by te.country

In the second part of the query, there is no alias defined for the inline view. You should name it and then refer to it.

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) > '01-JAN-2015'
  and te.plant = 'W'
  and te.country is null
  order by te.country) te --missing alias
group by te.country

Or you can just refer to the column name without the alias.

SELECT country, listagg(exception_date, ' ,') WITHIN GROUP (ORDER BY country) country
    FROM 
    (select unique te.exception_date, 'GB' country
      from tt_exception te
      where trunc(te.exception_date) > '01-JAN-2015'
      and te.plant = 'W'
      and te.country is null
      order by te.country)
    group by country
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) > '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) > '01-JAN-2015'
  and te.plant = 'W'
  and te.country is null
  order by te.country)--**you are missing the alias "te"**
group by te.country

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM