首页 > 解决方案 > MS Access(或 SQL)查询:等效于水平 CountIf()

问题描述

如果我有一组这样的值:

字段 1 字段 2 字段 3 字段 4
一个 C
2 4
X 是的

如果我想知道每行有多少非空值,在 Excel 中,我可以这样做:

=4 - CountIf(A2:D2, "")

有没有办法在 MS Access(或一般的 SQL)中的查询中做到这一点?

标签: sqlms-accesscountif

解决方案


您可以将它们添加到 MS Access 中:

select t.*,
       (iff(field1 is not null, 1, 0) + 
        iff(field2 is not null, 1, 0) + 
        iff(field3 is not null, 1, 0) + 
        iff(field4 is not null, 1, 0)
       ) as num_not_null
from t;

推荐阅读