使用LISTAGG时发生红色错误:必须在至少一个用户创建的表上应用一个或多个使用的函数

时间:2019-02-20 09:00:03

标签: amazon-redshift

我在redshift中使用listagg(),并且where条件为空,检查作为主键的bigint列。

查询:

从account_id为空的故障单中选择listagg(display_id);

并引发错误:

必须在至少一个用户创建的表上应用一个或多个使用的功能。仅用户表功能的示例包括LISTAGG,MEDIAN,PERCENTILE_CONT等

该列的详细信息是:

列|类型|整理|可空的

account_id | bigint | |不为空

目标是不获取特定account_id的数据 listagg()是否有替代方法或解决此问题的方法?

1 个答案:

答案 0 :(得分:0)

问题在于Redshift listagg在带有“ 总是”假的“ where”子句的查询中不起作用。有问题的account_id永远不会为null,因为表定义不允许它。

不幸的是,Redshift错误文本没有使它变得明显,但是在AWS论坛上有一个相关的主题:https://forums.aws.amazon.com/thread.jspa?threadID=242096

您可以通过在Redshift上运行以下示例来进行测试

-- Create a temp table with not null account_id

create table #ticket (display_id int, account_id bigint not null);
insert into #ticket values (1,1), (1,2), (2,4), (3, 5), (3,1);

-- This query like the example in the question fails because the where clause always evaluates to false
select listagg(display_id) from #ticket where account_id is null;

-- This fails with the same error
select listagg(display_id) from #ticket where false;

-- But both these example succeed
select listagg(display_id) from #ticket;
select listagg(display_id) from #ticket where true;

这里是相同的示例,但是这次表的确允许account_id的值为空,并且查询成功。

-- Create another temp table that allows null account_id

create table #ticket2 (display_id int, account_id bigint);
insert into #ticket2 values (1,1), (1,2), (2,null), (3, null), (3,1);

-- This query like the example in the question now suceeds
select listagg(display_id) from #ticket2 where account_id is null;

-- These also succeed
select listagg(display_id) from #ticket2;
select listagg(display_id) from #ticket2 where true;

-- This one fails with the same error
select listagg(display_id) from #ticket2 where false;