日期:2014-05-18  浏览次数:20523 次

高手帮我简化一下这句SQL,谢谢!
select   bureau_id,cust_name,sum(temp1),sum(temp2),sum(temp1)/sum(temp2)*100     from
(select   bureau_id,cust_name,count(acc_nbr)   as   temp1,0   temp2     from   wm_200701mon
  where   type= '201电话 '   and   card_charge=0
  group   by   bureau_id,cust_name
  union   all
  select   bureau_id,cust_name,0   temp1,count(acc_nbr)   as   temp2     from   wm_200701mon
  where   type= '201电话 '
  group   by   bureau_id,cust_name
)  
group   by   bureau_id,cust_name
;

我只是想得到一个card_charge为0的占比报表,帮帮我,谢谢!

------解决方案--------------------
select bureau_id,cust_name,temp1=sum(case when card_charge=0 then count(acc_nbr) else 0 end),temp2=sum(case when card_charge=0 then 0 else count(acc_nbr) end),temp3=sum(case when card_charge=0 then count(acc_nbr) else 0 end)/sum(case when card_charge=0 then 0 else count(acc_nbr) end)*100
from wm_200701mon where type= '201电话 ' group by bureau_id,cust_name