我需要编写一个Firebird存储过程来检查4个字段值,并仅返回非空值的计数。
例如伪代码:
X = 0; //is the count variable
if field_1 is not null then X = 1;
if field_2 is not null then X = X + 1;
if field_3 is not null then X = X + 1;
if field_4 is not null then X = X + 1;
但我想询问是否可以在一个Select中进行?
我正在使用Firebird 2.5
答案 0 :(得分:1)
您可以使用IIF
功能执行此操作,如下所示:
select IIF(Field_1 is null, 0, 1)
+ IIF(Field_2 is null, 0, 1)
+ IIF(Field_3 is null, 0, 1)
+ IIF(Field_4 is null, 0, 1)
from SomeTable;
答案 1 :(得分:0)
如果你想获得每行的总和,那么使用像Welliam建议的IIF函数就可以了。
但是如果你想在所有行上只获得一个总和,请使用带有显式字段名的聚合函数COUNT
select count(Field_1) + count(Field_2) + count(Field_3) + count(Field_4)
from SomeTable;
就像在这种情况下一样,它计算指定字段不为NULL的行。