sql - 如果任何源列为真,则将 bool 值聚合为真

标签 sql postgresql aggregate-functions boolean-logic

假设我有下表:

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

根据上表,我想:

select rows from id = 1,2;

结果应该是:

column_a   column_b   column_c
 t          f            t

如果定义的 id 中的任何行对于特定列为真,我们假定结果为真。

最佳答案

使用聚合函数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);

手册:

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

关于sql - 如果任何源列为真,则将 bool 值聚合为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40415401/

相关文章:

sql - SQLite查询聚合

sql - 如何使用 SQL 查询在 Access 中的字段上设置验证规则?

sql - 如何在 MYSQL 查询中将名字和姓氏作为全名?

sql - plpgsql 函数的动态部分

apache-spark - 用户定义的函数要应用于 PySpark 中的 Window?

sql - Sql 中的聚合

sql - 连接两个表,以便结果显示在多列中

sql - MySQL 日期字段不包含任何值 - 导入时出现问题

database - PG::ConnectionBad: 运行后无法转换主机名错误 export DATABASE_URL=postgres://$(whoami)

postgresql - 列出 Postgres 中包含具有 NULL 值的 bool 类型列的所有表?