一表如下
a   1   2   3    4   5   6
aaa t   t   t    f   t   t
bbb f   f   f    f   t   t
ccc f   t   t    t   t   t
要统计出1到6月份t的个数大于3的记录.

解决方案 »

  1.   

    select a,count(1) from (select a,1
    union all
    select a,2
    union all
    select a,3
    union all
    select a,4
    union all
    select a,5
    union all
    select a,6
    union all) k group by a having count(1)>3
      

  2.   

    动态 declare @sql varchar(1000)
    set @sql=''
    select @sql=@sql+ ' select a,'+[name]+' as countc from tablename union all'
    from syscolumns where object_id('tablename')=id 
    set @sql=left(@sql,len(@sql)-10)
    set @sql='select a,count(countc) from ('+@sql+') k group by a having count(countc)>3' print @sql
      

  3.   

    select a, tORf=(case when charindex('ttt',replace([1]+[2]+[3]+[4]+[5]+[6],'f',''),1)>0 then 'T' else 'F' end)
    from tb
      

  4.   

    select a, tORf=(case when len(replace([1]+[2]+[3]+[4]+[5]+[6],'f','')>=3 then 'T' else 'F' end)
    from tb