sql - 在 PostgreSQL 中的 where 语句中根据多个条件过滤行
问题描述
我在 Postgres 11.0 中有下表
id status
NCT00632827 true
NCT00632827 (null)
NCT00770185 false
NCT00770185 (null)
NCT00784303 (null)
NCT00784303 (null)
由于状态列中的值不同,id 值重复。我想保留对或错的行。如果多行有空值,我想只保留 1 行。所需的输出是:
id status
NCT00632827 true
NCT00770185 false
NCT00784303 (null)
我正在尝试以下查询:
select id, status
where status = 'false' or status = 'true'
from table
如何保留具有空值的行(所需输出中的最后一行)?
解决方案
对于这个数据集,简单的聚合似乎足以产生预期的结果:
select id, bool_or(status) status from mytable group by id
推荐阅读
- activemq - 使用代理网络的 ActiveMQ 消息排序
- powerbi - 是否有任何 DAX 表达式用于根据索引计算几行的 SUM?
- java - 创建一个重复循环,这样程序会询问用户是否需要另一个条目,直到用户指示“否”。
- symfony - 在 Symfony 的 Migration 类中更改数据库连接
- c# - 当我尝试使用它时,添加到字典中的动态创建的按钮会出现异常。WPF
- angular - 当我运行 ng test 时设置默认启动器标志而不使用其他参数
- php - 限制 API 访问仅适用于 Android 应用程序
- java - 在 glassfish5.0.1 中部署时出现无法添加类型侦听器的错误
- reactjs - 接口“NodeModule”不能同时扩展类型“Module”和“Module”
- fortran - 我无法在 Sublime 3 中写入 Fortran 程序的标准输入