sql - How to count different fields in a row in SQL
问题描述
In my table I have a list of fields I want to count when they are not NULL.
For example, here are some rows and what I want to count in the end:
| locks | keys | boxes | what_I_want_count |
| 3 | 4 | 5 | *12* |
| 2 | 0 | 7 | *9* |
| 0 | 0 | 1 | *1* |
Any idea how to do this? I am using Postgresql (Redshift).
解决方案
你可以使用coalesce()
函数然后添加你的列
select coalesce(locks, 0) + coalesce(keys, 0) + coalesce(boxes, 0) from tableA
推荐阅读
- python - 为什么在为 python 3.6 安装 ratfun 包时出现“错误命令错误,退出状态为 1”?
- postgresql - 无法从 pgAdmin 访问数据库
- angular - 如何在 Angular 8 中生成类的方法名称列表?
- apache-kafka - Kafka Connect Hbase 接收器
- excel - Excel 2010 和 2013 (OLEObject) 之间的 VBA 命令不兼容?
- performance - Selenium 中的并行自动化
- javascript - 是否可以通过 Optimizely 自定义代码引用 Tealium 的通用数据对象 (UDO)?
- python - Pandas Dataframe:IndexError:单个位置索引器超出范围
- r - R中nlm-package的时间复杂度?
- react-native - React Native onRegister 事件不起作用(iOS)