Aggregate boolean values to true if any of the source columns is true

半城伤御伤魂 提交于 2020-01-04 03:56:07

问题


Let say I have the following table:

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

From the above table, I want to:

select rows from id = 1,2;

The result should be:

column_a   column_b   column_c
 t          f            t

If any of the rows among the defined id has a true for a particular column we assume the result to be true.


回答1:


Use the aggregate function bool_or().

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

The manual:

true if at least one input value is true, otherwise false




回答2:


bool_or(...) is definitely your best friend here.

Just one thing to remember which manual doesn't mention: If all values supplied to bool_or are null, the result will be null too.



来源:https://stackoverflow.com/questions/40415401/aggregate-boolean-values-to-true-if-any-of-the-source-columns-is-true

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