Sql Firebird-使用select inside SP返回不等于null的字段计数
我需要编写一个Firebird存储过程来检查4个字段的值,并只返回不为null的字段的计数 例如,在伪代码中:Sql Firebird-使用select inside SP返回不等于null的字段计数,sql,stored-procedures,firebird,Sql,Stored Procedures,Firebird,我需要编写一个Firebird存储过程来检查4个字段的值,并只返回不为null的字段的计数 例如,在伪代码中: 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;
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;
但我想问,是否有可能在一个单一的选择内完成这项工作
我使用的是Firebird 2.5您可以使用
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;
如果您想得到每行的总和,那么使用Welliam建议的函数就可以了 但是,如果您只想在所有行上获得一个和,请使用带有显式字段名的聚合函数
COUNT
select count(Field_1) + count(Field_2) + count(Field_3) + count(Field_4)
from SomeTable;
在这种情况下,它对指定字段不为NULL的行进行计数。是的,这比使用两个函数和长语法
Sum(IIF…
…)要好