行中非空值的计数

时间:2012-04-09 18:47:56

标签: sql sqlite

有更好的方法吗?

select count(First)+count(id)+count(Last)+count(Telephone) 
from client 
where id ="1";

1 个答案:

答案 0 :(得分:1)

不确定这是否更好,但对于稍后阅读的人来说,意图可能会更清晰:

select 
  (case when First is null then 1 else 0 end) + 
  (case when id is null then 1 else 0 end) + 
  (case when Last is null then 1 else 0 end) + 
  (case when Telephone is null then 1 else 0 end) 
from client 
where id ="1";