[英]How to count different fields in a row in SQL
在我的表中,我有一個字段列表,當它們不是 NULL 時,我想計算它們。
例如,這里有一些行以及我想最后計算的內容:
| locks | keys | boxes | what_I_want_count |
| 3 | 4 | 5 | *12* |
| 2 | 0 | 7 | *9* |
| 0 | 0 | 1 | *1* |
知道怎么做嗎? 我正在使用 Postgresql(紅移)。
你可以使用coalesce()
function 然后添加你的列
select coalesce(locks, 0) + coalesce(keys, 0) + coalesce(boxes, 0) from tableA
如果所有 3 列的數據類型為 integer 或 bigint,您可以進行以下查詢
select locks,keys,boxes,(locks,keys,boxes) as total_count from T1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.