Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create array for values from list of columns extracted in Postgres

I have following set of table:

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

Which when queried by:

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);

gives result as:

column_a   column_b   column_c
 t          f            t

I wanted to get the array from the result as: [t,f,t] in Postgres.

Please have reference to the earlier stack question over here.

like image 333
Syed Asad Abbas Zaidi Avatar asked Nov 04 '16 05:11

Syed Asad Abbas Zaidi


1 Answers

Use an ARRAY constructor:

SELECT ARRAY [bool_or(column_a)
            , bool_or(column_b)
            , bool_or(column_c)] AS arr_abc
FROM   tbl
WHERE  id IN (1,2);
like image 102
Erwin Brandstetter Avatar answered Sep 29 '22 11:09

Erwin Brandstetter