字符串“(LISTAGG 结果)”太长,会被截断 - 如何解决这个问题?

String '(LISTAGG result)' is too long and would be truncated - How to fix this?

SELECT DISTINCT B.ID, LISTAGG(D.PROCESS_ID,',')
FROM table1 A
JOIN table2 B
ON A.ST_ID = B.ST_ID
JOIN table2 C
ON A.ST_ID = C.ST_ID
JOIN table4 D
ON A.ST_ID = C.ST_ID
WHERE B.DATE = '2022-02-02'
AND FL_CD NOT IN ('1','2','3','4','5')
GROUP BY 1

当我 运行 上面的代码时,我得到了这个错误 - String '(LISTAGG result)' is too long and would be t运行cated。这是因为 PROCESS_ID 列具有巨大的价值。请提供解决此问题的解决方案。

正如许多答案所呼唤的那样,可能有一个爆炸式连接正在创建比预期更多的数据。

无论如何,我在 JavaScript 中创建了一个 UDTF,它可以接收 table 并为每个标签提取有限数量的样本元素——这基本上就是问题所要求的:

create or replace function limited_array_agg(arr_limit float, g string, s string)
returns table (G string, S array)
language javascript
as $$
{
    processRow: function f(row, rowWriter, context){
        if(this.counter < row.ARR_LIMIT){
            this.arr.push(row.S)
            this.group = row.G
            this.counter++;
        };
    }        
    , initialize: function(argumentInfo, context) {
        this.counter = 0;
        this.arr = [];
    }, finalize: function(rowWriter, context){
        rowWriter.writeRow({G:this.group, S: this.arr})
    }
}
$$;

您可以像这样使用它,您的查询可以是 CTE:

select limited_agg.g, limited_agg.s
from snowflake_sample_data.tpch_sf100.part
    , table(limited_array_agg(
        3::float
        , p_mfgr
        , p_name) over(partition by p_mfgr)) limited_agg
;

与此同时:我希望 Snowflake 有一个原生的 array_agg() 来限制元素的数量。