繁体   English   中英

Oracle sql - 无效的标识符LISTAGG

[英]Oracle sql - Invalid Identifier 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

在查询的第二部分中,没有为内联视图定义别名。 你应该命名它,然后引用它。

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
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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