设表有十个字段,
如何查询
任意两个字段及以上不为空的记录?

解决方案 »

  1.   

    select * from tb where 
    (case when c1 is not null then 1 else 0 end) + 
    (case when c2 is not null then 1 else 0 end) + 
    (case when c3 is not null then 1 else 0 end) + 
    (case when c4 is not null then 1 else 0 end) + 
    (case when c5 is not null then 1 else 0 end) + 
    (case when c6 is not null then 1 else 0 end) + 
    (case when c7 is not null then 1 else 0 end) + 
    (case when c8 is not null then 1 else 0 end) + 
    (case when c9 is not null then 1 else 0 end) + 
    (case when c10 is not null then 1 else 0 end) >= 2
      

  2.   

    declare @sql varchar(8000)select @sql = isnull(@sql+'+(case isnull(','')+name+','''') when '''' then 0 else 1 end)'
    from syscolumns where id=object_id('tb')select @sql ='select * from tb where (case isnull('+@sql+'>=2'exec(@sql)
      

  3.   

    ;with cte as (
    select id = row_number() over (order by col1),
      num =  isnull(nullif(isnull(col1  , 1),col1),1)+ isnull(nullif(isnull(col2  , 1),col2),1)...
    )select * from cte where num>2
    也可以用case when哦, 理解错了, 我是找的null >2
      

  4.   

    完全错了, 你自己写CASE WHEN吧
      

  5.   


    declare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+'+(case isnull('+ name +','''') when '''' then 0 else 1 end)' 
    from syscolumns where id=object_id('tb')
    set @sql='select * from tb where '+ stuff(@sql,1,1,'') +'>2'
    exec(@sql)