select  riqi,gongsibie
,count(*) as '检验批'
,sum(case panding when 'ok' then 0 else 1 end) as '批退批'
,rtrim(cast(sum(case panding when 'ok' then 0 else 1 end)*1.0/count(*) as decimal(18,2)))+'%' as '批退率'
from wai
group by riqi,gongsibie

解决方案 »

  1.   


    select 日期=riqi,
           公司别=gongsibie,
    检验批=count(*),
      检验批=sum(case when panding<>'ok' then 1 else o),
            批退率=cast(1.00*sum(case when panding<>'ok' then 1 else o)/count(*) as varchar(10))+'%'
    from tb
    group by riqi,gongsibie
      

  2.   

    select  riqi,gongsibie
            ,检验批=count(*)
            ,批退批=sum(case when panding ='ng' then 1 else 0 end)
            ,批退率=rtrim(cast(sum(case panding when 'ng' then 1 else 0 end)*1.0/count(*) as decimal(18,2)))+'%'
    from wai
    group by riqi,gongsibie
      

  3.   

    强,厉害,小弟佩服的五体投地,小弟对sql语句只懂简单的查询,太谢谢了,要不是你,我就要考虑使用c#写循环来实现了,