2012-04-09 101 views
0

有没有更好的方法来做到这一点?连续计数非空值

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

你到底想干什么? – theglauber 2012-04-09 19:03:52

+0

获得一行中非空值的计数 – adayzdone 2012-04-09 19:05:22

+0

我也会对行中空值的计数感到满意。 – adayzdone 2012-04-09 19:12:36

回答

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"; 
+0

这看起来更清洁。我正在寻找一个解决方案,但我并不需要拼出每一列。谢谢。 – adayzdone 2012-04-09 19:11:56

+0

当然 - 不用担心。我知道如何在tsql中做到这一点;它需要是SQLite吗? – Geoff 2012-04-09 20:05:24