SQL用什么方法统计出一行数据里边有几个空值比较好?

解决方案 »

  1.   

    1、sum(case when ... then 1 else 0)+(...) 统计每一列2、将表数据列转行,group by 统计。
      

  2.   

    这个意思?
    select 
    (case when 列1 is null then 1 else 0 end)+
    (case when 列2 is null then 1 else 0 end)+
    (case when 列3 is null then 1 else 0 end)+
    (case when 列4 is null then 1 else 0 end)+
    ....
    from tb
      

  3.   

    sum(case when col is null then 1 else 0)...
      

  4.   


    create table tb 
    (
    col1 varchar(10),
    col2 varchar(10),
    col3 varchar(10),
    col4 varchar(10),
    col5 varchar(10)
    )insert into tb
    select 'col',null,null,'col4','col5'GO
    declare @String varchar(max)
    SELECT @String=CONVERT(varchar(20),isnull(col1,'@'))+CONVERT(varchar(20),isnull(col2,'@'))+CONVERT(varchar(20),isnull(col3,'@'))
          +CONVERT(varchar(20),isnull(col4,'@'))+CONVERT(varchar(20),isnull(col5,'@'))
    from tbselect datalength(@String)-datalength(REPLACE(@String,'@','')) as 'NULL值个数'GO
    DROP TABLE TB