首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

高手帮小弟我简化一下这句SQL,多谢

2012-02-04 
高手帮我简化一下这句SQL,谢谢!selectbureau_id,cust_name,sum(temp1),sum(temp2),sum(temp1)/sum(temp2)*

高手帮我简化一下这句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

热点排行
Bad Request.