简体   繁体   中英

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

I am trying to use the listagg() function to create a comma-separated list of document URLs, and it causes the ORA-01489 "result of string concatenation too long" error. So I attempted to correct this by inserting an "on overflow" clause as in the following snippet:

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

...but when I attempt to run the SQL statement, I get an "ORA-00907 missing right parenthesis" error - apparently because SQLdeveloper doesn't expect to see the "on" keyword inside the first set of parentheses. However, all of my Google searches indicate my syntax is correct. Can anybody spot what I may be doing wrong?

I suspect that you are running Oracle < 12.2, where the on overflow clause is not available. An alternative is to use a cumulative sum() of the length of the strings, in bytes, and use that to limit the output:

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

Notes:

  • for this to make sense, you need an ordering coumn that is different than the partitioning column; I assumed id

  • I changed the outer query to an aggregation query; presumably, that's your actual intent, rather than window aggregation and distinct

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