【发布时间】:2021-08-21 17:00:17
【问题描述】:
Redhisft listagg DISTINCT 函数是否可能存在错误?
当我尝试这个时
select
case when partition_field IS NOT NULL THEN regexp_count(
listaggdistinct(trim(counted_field), ',') over (partition by partition_field ),
','
) + 1 END AS counted_distinct
from
TABLE
我收到消息错误:结果大小超过 LISTAGG 限制代码:8001
但是 我正在连接最大长度为 18 的 varchar 字段 已通过此查询验证
select max(length(counted_field)) from TABLE
如果我运行以下命令,我会得到 10 ...
select max(COUNTER) from
(SELECT case when partition_field IS NOT NULL THEN
count( counted_field) over (partition by partition_field )
END as COUNTER
from TABLE
)x
所以 10*18 ... 比 65K 小得多!!!
我做错了什么?
请注意,我与此代码的结果完全相同 这与 Redshift 文档 100% 一致
select
case when partition_field IS NOT NULL THEN regexp_count(
listagg( distinct counted_field, ',') within group (order by counted_field) over (partition by partition_field ),
','
) + 1 END AS counted_distinct
from
TABLE
在这段代码中,within group (order by contact_id) 存在只是因为否则 Redshift 会返回 WINDOW 错误
【问题讨论】:
-
也许它被定义为
char()而不是varchar()并且它被空格填充。 -
我只是尝试将 counted_field 显式转换为 varchar(18) ... 没有改进另外我用 RIGHT 函数做了几次测试,它从 5 开始用 RIGHT(counted_field,5) 我失败了检查特殊字符 ``` select * from( select counted_field,REGEXP_COUNT(counted_field , '^[a-zA-Z0-9]{18}$') as test from TABLE ) where test!=1 ... 一无所获```
标签: sql amazon-redshift listagg