当没有输入行时,让array_agg()不返回任何行

问题描述 投票:0回答:1

当没有行时,函数array_agg()返回null。如何更改此项以便不返回任何行?例如,采用以下简单查询:

select array_agg(country)
from   country
where  false

这将返回值为null的一行。我想没有返回任何行。

postgresql
1个回答
2
投票

使用HAVING子句删除空数组:

[local] #= select array_agg(1) from a where false;
┌───────────┐
│ array_agg │
├───────────┤
│ (null)    │
└───────────┘
(1 row)

[local] #= select array_agg(1) from a where false having array_agg(1) <> '{}';
┌───────────┐
│ array_agg │
├───────────┤
└───────────┘
(0 rows)
© www.soinside.com 2019 - 2024. All rights reserved.