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

SQL实在想不同
这是表结构:
incoming ppname pname name 数量  
电话销售 售后 产品 使用方法 20  
电话销售 售后 产品 效果 18  
电话销售 售后 产品 营养健康 5  
电话销售 售后 服务 其他 5  
电话销售 售后 媒体 广告 1  
电话销售 售后 媒体 赞助 1  
电话销售 售后 政策 其他 21  
电话销售 售后 政策 退换货政策 2  
电话销售 售后 政策 退款政策 1  
电话销售 售后 政策 物流政策 42  
电话销售 售前 产品 包装 19  


这是我想的到数据 不用百分比显示也可以 如47% 显示: 0.46511


incoming ppname pname name 数量 百分比
电话销售 售后 产品 使用方法 20 47%
电话销售 售后 产品 效果 18 42%  
电话销售 售后 产品 营养健康 5 12%
电话销售 售后 服务 其他 5 100%
电话销售 售后 媒体 广告 1 50%
电话销售 售后 媒体 赞助 1 50%
电话销售 售后 政策 其他 21 32%
电话销售 售后 政策 退换货政策 2 2%
电话销售 售后 政策 退款政策 1 3%  
电话销售 售后 政策 物流政策 42 64%
电话销售 售前 产品 包装 19 100%

------解决方案--------------------
SQL code

--incoming ppname pname name 数量

select a.*,ltrim(cast(a.數量*100./nullif(b.數量,0) as decimal(12,2)))+'%' as 百分比
from tb a 
  outer apply 
    (select sum(數量) 數量 from tb where incoming = a.incoming and ppname = a.ppname and name = a.name) b

------解决方案--------------------
是不是想要这个结果啊?
SQL code
select a.incoming,a.ppname,a.pname,a.name,a.qty,round(a.qty/b.qty,4) as 百分比
from table a,(
select incoming,ppname,pname,SUM(qty) qty from table 
group by incoming,ppname,pname) b
where a.incoming=b.incoming and a.ppname=b.ppname and a.pname=b.pname

------解决方案--------------------
SQL code
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([incoming] varchar(8),[ppname] varchar(4),[pname] varchar(4),[name] varchar(10),[数量] int)
insert [tb]
select '电话销售','售后','产品','使用方法',20 union all
select '电话销售','售后','产品','效果',18 union all
select '电话销售','售后','产品','营养健康',5 union all
select '电话销售','售后','服务','其他',5 union all
select '电话销售','售后','媒体','广告',1 union all
select '电话销售','售后','媒体','赞助',1 union all
select '电话销售','售后','政策','其他',21 union all
select '电话销售','售后','政策','退换货政策',2 union all
select '电话销售','售后','政策','退款政策',1 union all
select '电话销售','售后','政策','物流政策',42 union all
select '电话销售','售前','产品','包装',19
go

;with cte1 as(
select rn=row_number() over(order by getdate()),* from tb
),
cte2 as(
select gid=rn-(select count(1) from cte1 where pname=t.pname and rn<t.rn),*
from cte1 t
)
select a.incoming,a.ppname,a.pname,a.name,a.数量,百分比=ltrim(cast(a.数量*100.0/b.数量 as dec(18,2)))+'%' 
from cte2 a
join (select gid,sum(数量) as 数量 from cte2 group by gid) b 
on a.gid=b.gid
/**
incoming ppname pname name       数量          百分比
-------- ------ ----- ---------- ----------- ------------------------------------------
电话销售     售后     产品    使用方法       20          46.51%
电话销售     售后     产品    效果         18          41.86%
电话销售     售后     产品    营养健康       5           11.63%
电话销售     售后     服务    其他         5           100.00%
电话销售     售后     媒体    广告         1           50.00%
电话销售     售后     媒体    赞助         1           50.00%
电话销售     售后     政策    其他         21          31.82%
电话销售     售后     政策    退换货政策      2           3.03%
电话销售     售后     政策    退款政策       1           1.52%
电话销售     售后     政策    物流政策       42          63.64%
电话销售     售前     产品    包装         19          100.00%

(11 行受影响)
**/

------解决方案--------------------
SQL code

select a.incoming,a.ppname,a.pname,a.name,a.amount,a.amount * 1.0 / b.amount as '百分比'
from #t a 
    left join (select incoming, ppname,pname,sum(amount) amount
    from #t 
    group by incoming, ppname,pname) b
on a.incoming = b.incoming
and a.ppname = b.ppname
and a.pname = b.pname

------解决方案--------------------