为 Postgres 中提取的列列表中的值创建数组

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

我有以下一组表格:

id   column_a  column_b   column_c
1     t          f           t
2     t          f           f
3     f          t           f

当查询时:

SELECT bool_or(column_a) AS column_a
     , bool_or(column_b) AS column_b
     , bool_or(column_c) AS column_c
FROM   tbl
WHERE  id IN (1,2);

给出的结果为:

column_a   column_b   column_c
 t          f            t

我想从结果中获取数组:Postgres 中的

[t,f,t]

请参考此处之前的堆栈问题。

sql arrays postgresql aggregate-functions
1个回答
7
投票

使用 ARRAY 构造函数:

SELECT ARRAY [bool_or(column_a)
            , bool_or(column_b)
            , bool_or(column_c)] AS arr_abc
FROM   tbl
WHERE  id IN (1,2);
© www.soinside.com 2019 - 2024. All rights reserved.