Oracle sql - недопустимый идентификатор LISTAGG

Я новичок в использовании функции LISTAGG. Когда я запускаю следующий сценарий, я получаю ошибку неверного идентификатора:

ORA-00904: «TE». «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)
group by te.country

person SMORF    schedule 28.01.2016    source источник
comment
Какую версию Oracle вы используете?   -  person Mureinik    schedule 28.01.2016
comment
Функцию LISTAGG можно использовать только в следующих версиях Oracle / PLSQL: Oracle 12c, Oracle 11g Release 2.   -  person Haytem BrB    schedule 28.01.2016
comment
есть ли в таблице tt_exception столбец с именем country?   -  person Florin Ghita    schedule 28.01.2016


Ответы (1)


Во второй части запроса для встроенного представления не определен псевдоним. Вы должны назвать его, а затем сослаться на него.

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

Или вы можете просто сослаться на имя столбца без псевдонима.

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
person Vamsi Prabhala    schedule 28.01.2016