Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 统计表行中的空列数_Sql_Sql Server_Sql Server 2008_Null - Fatal编程技术网

Sql 统计表行中的空列数

Sql 统计表行中的空列数,sql,sql-server,sql-server-2008,null,Sql,Sql Server,Sql Server 2008,Null,可能重复: 我的表有五列: Name DOB Email phone jobtitle abc null a@c.com null null bbc null null null null 姓名DOB电子邮件电话职务 abc空a@c.com空值 英国广播公司空 如何编写查询以便在一行中查找空列数 (例如,第1行有3个空值,第2行有4个空值。) 我正在使用SQLServer2008 天真的方式:

可能重复:

我的表有五列:

Name DOB Email phone jobtitle abc null a@c.com null null bbc null null null null 姓名DOB电子邮件电话职务 abc空a@c.com空值 英国广播公司空 如何编写查询以便在一行中查找空列数

(例如,第1行有3个空值,第2行有4个空值。)

我正在使用SQLServer2008

天真的方式:

SELECT CASE WHEN Name IS NULL THEN 1 ELSE 0 END +
    CASE WHEN DOB IS NULL THEN 1 ELSE 0 END +       
    CASE WHEN Email IS NULL THEN 1 ELSE 0 END +       
    CASE WHEN phone IS NULL THEN 1 ELSE 0 END +       
    CASE WHEN jobtitle IS NULL THEN 1 ELSE 0 END
但我不想写其中的76篇。那么动态方式如何(未经测试,但大致如下):


我认为唯一的方法是指定每个列

select sum(case when item1 is null then 1 else 0 end
          +case when item2 is null then 1 else 0 end
          +case when item3 is null then 1 else 0 end
          +case when item4 is null then 1 else 0 end
          +case when item5 is null then 1 else 0 end
          +case when item6 is null then 1 else 0 end
          ) as grandtotalnulls
  from yourtable

欢迎问题在哪里?你试过什么?我有一张76栏的在线表格。如果候选人只填写30份表格,那么我必须显示表格的百分比。这就是一个例子。如果在SQL Server中以这种方式进行查询时,无效,则我的表中有76列。@AaronBertrand是的,是的,你说得很对。有很多方法可以做到这一点,而不必编写76个
CASE
表达式(这与之前的解决方案有何不同?)。请
select sum(case when item1 is null then 1 else 0 end
          +case when item2 is null then 1 else 0 end
          +case when item3 is null then 1 else 0 end
          +case when item4 is null then 1 else 0 end
          +case when item5 is null then 1 else 0 end
          +case when item6 is null then 1 else 0 end
          ) as grandtotalnulls
  from yourtable