Create array for values from list of columns extracted in Postgres

纵然是瞬间 提交于 2019-12-10 09:28:11

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/40415894/create-array-for-values-from-list-of-columns-extracted-in-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!