![](/img/trans.png)
[英]SQL select all rows with the same ticker where at least one of the rows includes a specific value
[英]SQL: Need to select rows where at least 3 columns are not equal to specific value
我需要选择至少三列不等于“未知”的行
示例数据集:
id condition_A condition_B condition_C condition_D Condition_E
1 Unknown Positive Negative Negative Unknown
2 Unknown Unknown Negative Negative Unknown
3 Positive Positive Negative Negative Unknown
在这种情况下,我希望查询返回第 1 行和第 3 行。
我认为这会做到:
select * from tablename
where 3 <=
(condition_A <> 'Unknown')::int +
(condition_B <> 'Unknown')::int +
(condition_C <> 'Unknown')::int +
(condition_D <> 'Unknown')::int +
(condition_E <> 'Unknown')::int
请参阅演示。
结果:
| id | condition_a | condition_b | condition_c | condition_d | condition_e |
| --- | ----------- | ----------- | ----------- | ----------- | ----------- |
| 1 | Unknown | Positive | Negative | Negative | Unknown |
| 3 | Positive | Positive | Negative | Negative | Unknown |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.