Oracle 12c:尝试在 SQLdeveloper 中使用时,listagg 函数无法识别 "on overflow" 子句

Oracle 12c: listagg function does not recognize the "on overflow" clause when attempting to use in SQLdeveloper

我正在尝试使用 listagg() 函数创建一个逗号分隔的文档 URL 列表,它会导致 ORA-01489“字符串连接结果太长”错误。所以我试图通过插入一个“溢出”子句来纠正这个问题,如下面的片段:

  select distinct
    wo_id,
    listagg(document_id, ',' ON OVERFLOW TRUNCATE WITH COUNT) 
      within group (order by wo_id) over (partition by wo_id) as document_ids
  from
  (
    <...inner SELECT result set...>
  )

...但是当我尝试 运行 SQL 语句时,我收到“ORA-00907 缺少右括号”错误 - 显然是因为 SQL 开发人员没有期望在第一组括号内看到“on”关键字。但是,我的所有 Google 搜索都表明我的语法是正确的。谁能发现我做错了什么?

我怀疑你是运行 Oracle < 12.2,其中on overflow子句不可用。另一种方法是使用字符串长度的累积 sum(),以字节为单位,并使用它来限制输出:

select wo_id, 
    listagg(document_id) within group (order by id) document_ids
from (
    select t.*, sum(lengthb(document_id)) over(partitionb by wo_id order by id) sum_len
    from mytable t
) t
where sum_lengthb < 4000
group by wo_id

备注:

  • 为了使这个有意义,您需要一个不同于分区列的排序列;我假设 id

  • 我把外层查询改成了聚合查询;据推测,这是您的实际意图,而不是 window 聚合和 distinct