1

似乎“ON OVERFLOW TRUNCATE”功能在 Oracle 中与 LISTAGG 一起使用的雪花中不可用。是否有替代功能或解决方法?

4

2 回答 2

1
select listagg(str, ', ') within group (order by id) as listagg
from (
    select id
        ,str
        ,sum(length(str))over(order by id) as s_str
    from (
        select * from values(1,'abcdef'),(2,'dfgsdfh'),(3,'jgeg'),(4,'ergegr') s(id,str)
    )
    qualify s_str <= 20
);

给出总计的值(在分隔符之前)

,sum(length(str)+2)over(order by id) as s_str

允许您考虑分隔符 ', ' 的大小

或者如果你想要一些截断的后缀,'...'那么这将做到这一点..

select listagg(str, ', ') within group (order by id) as listagg
from (
    select id
        ,s_str <= 20 as underlimit
        ,iff(underlimit, str, '...') as str
    from (
        select id
            ,str
            ,sum(length(str)+2)over(order by id) as s_str
        from (
            select * from values(1,'abcdef'),(2,'dfgsdfh'),(3,'jgeg'),(4,'ergegr') s(id,str)
        )
    )
    qualify underlimit or row_number() over (partition by underlimit order by id) = 1
);

给予

LISTAGG
'abcdef, dfgsdfh, ...'

重写为不会改变任何东西的 CTE...

with data as (
    select id
       ,str
       ,sum(length(str)+2)over(order by id) as s_str 
    from values 
        (1,'abcdef'),
        (2,'dfgsdfh'),
        (3,'jgeg'),
        (4,'ergegr') 
        s(id,str)
),  check_length_and_limit as (
    select id
        ,s_str <= 20 as underlimit
        ,iff(underlimit, str, '...') as str
    from data
    qualify underlimit or row_number() over (partition by underlimit order by id) = 1
)
select listagg(str, ', ') within group (order by id) as listagg
from check_length_and_limit;
于 2020-04-11T03:20:27.807 回答
0

不,Snowflake 不提供该选项。两条评论可能有助于解释为什么会这样:

  1. 定义具有长度限制的字符串字段(文本、varchar 等)没有性能或存储问题。因此,您可以定义非常大的列来处理这个问题。
  2. 这可以通过简单地使用 LISTAGG() 函数周围的 LEFT() 函数来轻松处理,以根据字段定义的大小进行限制,如果您已对其进行限制或您的数据超出了 Snowflake 字符串字段的限制。
于 2020-04-10T18:40:29.887 回答