首页 > 解决方案 > 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).

标签: sqlpostgresqlamazon-redshift

解决方案


你可以使用coalesce()函数然后添加你的列

select coalesce(locks, 0) + coalesce(keys, 0) +  coalesce(boxes, 0) from tableA

推荐阅读