select a.serviceID,a.servicetype,
  sum(case b.Isok when 1 then 1 else 0 end) ok,
  sum(case b.Isok when 0 then 1 else 0 end) un,
  sum(case when b.Isok=0 or b.Isok=1 then 1 else 0 end) [count],
  sum(case b.Isok when 1 then 1 else 0 end)/sum(case when b.Isok=0 or b.Isok=1 then 1 else 0 end) 'ok/count',
  sum(case b.Isok when 0 then 1 else 0 end)/sum(case when b.Isok=0 or b.Isok=1 then 1 else 0 end) 'unok/count'
from tbservicetype a left join tbservice b on a.serviceid=b.serviceid
group by a.serviceID,a.servicetype

解决方案 »

  1.   

    上面不够严谨,没有考虑除数为0 的情况,改成下面的清楚些:
    select a.serviceID,a.servicetype,
      sum(case b.Isok when 1 then 1 else 0 end) ok,
      sum(case b.Isok when 0 then 1 else 0 end) unok,
      sum(case when b.Isok=0 or b.Isok=1 then 1 else 0 end) [count],
      case sum(case b.Isok when 1 then 1 else 0 end)
        when 0 then 0
        else sum(case b.Isok when 1 then 1 else 0 end)
            /sum(case when b.Isok=0 or b.Isok=1 then 1 else 0 end)
      end 'ok/count',
      case sum(case b.Isok when 0 then 1 else 0 end)
        when 0 then 0
        else sum(case b.Isok when 0 then 1 else 0 end)
            /sum(case when b.Isok=0 or b.Isok=1 then 1 else 0 end)
      end 'unok/count'
    from tbservicetype a left join tbservice b on a.serviceid=b.serviceid
    group by a.serviceID,a.servicetype
      

  2.   

    TO:pbsql(风云)现在还是不能实现 count/allcount这项, count/allcount表示每种服务类型同表tbservice记录总和的比。另外用临时表是不是更好些,请继续指点!
      

  3.   


    select a.serviceID,a.servicetype,
    sum(case b.Isok when 1 then 1 else 0 end) ok,
    sum(case b.Isok when 0 then 1 else 0 end) unok,
    sum(1) [count],
    case sum(1) when 0 then 0
    else cast(cast(
    sum(case b.Isok when 1 then 100.0 else 0.0 end)
    /sum(1) as decimal(10,2)) as varchar)+'%'
    end as [ok/count],
    case sum(1) when 0 then 0
    else cast(cast(
    sum(case b.Isok when 0 then 100.0 else 0.0 end)
    /sum(1) as decimal(10,2)) as varchar)+'%'
    end as [unok/count],
    case (select count(*) from tbservice) when 0 then 0
    else cast(cast(
    sum(100.0)
    /(select count(*) from tbservice) 
    as decimal(10,2)) as varchar)+'%'
    end as [count/allcount]
    from tbservicetype a 
    left join tbservice b on a.serviceid=b.serviceid
    group by a.serviceID,a.servicetype
      

  4.   

    --或者:
    select a.serviceID,a.servicetype,
    sum(case b.Isok when 1 then 1 else 0 end) ok,
    sum(case b.Isok when 0 then 1 else 0 end) unok,
    sum(1) [count],
    case sum(1) when 0 then 0
    else cast(cast(
    sum(case b.Isok when 1 then 100.0 else 0.0 end)
    /sum(1) as decimal(10,2)) as varchar)+'%'
    end as [ok/count],
    case sum(1) when 0 then 0
    else cast(cast(
    sum(case b.Isok when 0 then 100.0 else 0.0 end)
    /sum(1) as decimal(10,2)) as varchar)+'%'
    end as [unok/count],
    case isnull(c.allcount,0) when 0 then 0
    else cast(cast(
    sum(100.0)
    /c.allcount 
    as decimal(10,2)) as varchar)+'%'
    end as [count/allcount]
    from tbservicetype a 
    left join tbservice b on a.serviceid=b.serviceid
    ,(select allcount=count(*) from tbservice) c
    group by a.serviceID,a.servicetype
      

  5.   

    --写掉了,加上如下:
    select a.serviceID,a.servicetype,
      sum(case b.Isok when 1 then 1 else 0 end) ok,
      sum(case b.Isok when 0 then 1 else 0 end) unok,
      sum(case when b.Isok=0 or b.Isok=1 then 1 else 0 end) [count],
      case sum(case b.Isok when 1 then 1 else 0 end)
        when 0 then 0
        else sum(case b.Isok when 1 then 1 else 0 end)
            /sum(case when b.Isok=0 or b.Isok=1 then 1 else 0 end)
      end 'ok/count',
      case sum(case b.Isok when 0 then 1 else 0 end)
        when 0 then 0
        else sum(case b.Isok when 0 then 1 else 0 end)
            /sum(case when b.Isok=0 or b.Isok=1 then 1 else 0 end)
      end 'unok/count',
      case sum(case when b.Isok=0 or b.Isok=1 then 1 else 0 end)
        when 0 then 0
        else sum(case when b.Isok=0 or b.Isok=1 then 1 else 0 end)
            /(select count(*) from serviceID)
      end 'count/allcount'
    from tbservicetype a left join tbservice b on a.serviceid=b.serviceid
    group by a.serviceID,a.servicetype