【问题标题】:Oracle sql - Invalid Identifier LISTAGGOracle sql - 标识符 LISTAGG 无效
【发布时间】:2016-01-28 14:54:59
【问题描述】:

我是使用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

【问题讨论】:

  • 您使用的是什么版本的 Oracle?
  • LISTAGG函数只能在以下Oracle/PLSQL版本中使用:Oracle 12c, Oracle 11g Release 2
  • tt_exception 表中是否有名为country 的列?

标签: sql oracle listagg


【解决方案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

【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多