select kaqinid,
上午早退=sum(case angther when '上午早退了' then yeid end),
上午迟到=sum(case angther when '上午迟到了' then yeid end),
下午早退=sum(case angther when '下午早退了' then yeid end),
下午迟到=sum(case angther when '下午迟到了' then yeid end)
from kaqin
group by kaqinid

解决方案 »

  1.   

    select
        kaoqinid,
        上午早退=sum(case yeid when 0 then 1 else 0 end),
        上午迟到=sum(case yeid when 1 then 1 else 0 end),
        下午早退=sum(case yeid when 2 then 1 else 0 end),
        下午迟到=sum(case yeid when 3 then 1 else 0 end)
    from 表 
    group by kaoqinid
      

  2.   

    create table aa(angther varchar(20),yeid int,kaqinid int)
    insert aa select '上午早退了',0,111111
    union all select '下午迟到了',2,222222
    union all select '下午迟到了',2,222222
    union all select '下午迟到了',2,222222
    union all select '上午迟到了',1,111111
    union all select '上午早退了',0,111111
    union all select '上午早退了',0,111111
    union all select '下午迟到了',2,222222
    union all select '下午早退了',3,222222
    union all select '下午早退了',3,111111
    union all select '下午迟到了',3,222222
    union all select '下午早退了',3,222222
    union all select '下午早退了',3,222222
    union all select '下午迟到了',2,111111
    union all select '下午早退了',3,111111
    union all select '下午早退了',3,111111
    union all select '下午迟到了',2,222222
    union all select '下午早退了',3,222222declare @s varchar(8000)
    set @s='select kaqinid'
    select @s=@s+',['+angther+']=count(case angther when '''+angther+''' then 1 else 0 end)' from aa group by angther
    exec(@s+'from aa group by kaqinid')
      

  3.   

    declare @s varchar(8000)
    set @s='select kaqinid'
    select @s=@s+',['+angther+']=sum(case angther when '''+angther+''' then 1 else 0 end)' from aa group by angther
    exec(@s+'from aa group by kaqinid')错了,应该是这样的