select sum(case when 项1字段 is not null then 1 else 0 end),
sum(case when 项2字段 is not null then 1 else 0 end),
...
sum(case when 项26字段 is not null then 1 else 0 end)
from t

解决方案 »

  1.   

    用case可以实现对null和‘’的判断么?没有用过
      

  2.   

    create table test(col1 varchar(10),col2 varchar(10))
    insert test select null,'a'
    union all select 'b','c'select total=col1+col2,col1,col2 from
    (
    select col1=sum(case when col1 is not null then 1 else 0 end),
    col2=sum(case when col2 is not null then 1 else 0 end)
    from test
    )adrop table testtotal       col1        col2        
    ----------- ----------- ----------- 
    3           1           2
      

  3.   

    CASE好像不能实现对is not null AND <> ‘’的判断吧,我是这么写的SELECT SUM(CASE 
    WHEN CEO IS NOT NULL THEN 1 
    WHEN CEO <> '' THEN 1 
    ELSE 0 
    END)
    FROM tSELECT COUNT(*) FROM t
    WHERE CEO IS NOT NULL AND CEO <> ''两条语句出的结果不一样
      

  4.   

    SELECT SUM(CASE 
    WHEN CEO IS NOT NULL and CEO<>'' THEN 1 
    ELSE 0 
    END)
    FROM t
      

  5.   

    顶楼上,建议楼主多看看帮助CASE 搜索函数:
    按指定顺序为每个 WHEN 子句的 Boolean_expression 求值。
    返回第一个取值为 TRUE 的 Boolean_expression 的 result_expression。 
    如果没有取值为 TRUE 的 Boolean_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。 照你的写法,''非空,当然会返回1了,根本不会进行<>''的判断,结果怎么可能对。