if not object_id('tb') is null drop table tb Go Create table tb([customer] int,[amount] int) Insert tb select 1,10 union all select 1,40 union all select 2,23 union all select 3,56 union all select 3,35 union all select 4,6 Go Select [customer], sum([amount])总额, ltrim(cast(sum([amount])*100.0/(select sum([amount])from tb) as dec(18,2)))+'%'百分比 from tb group by [customer] /* customer 总额 百分比 ----------- ----------- ------------------------------------------ 1 50 29.41% 2 23 13.53% 3 91 53.53% 4 6 3.53% */
select 客户,总额=sum(amount), 百分比=ltrim(cast(总额*100.0/(select sum(amount) from tb) as decimal(10,1)))+'%' from tb group by 客户
drop table tb
Go
Create table tb([customer] int,[amount] int)
Insert tb
select 1,10 union all
select 1,40 union all
select 2,23 union all
select 3,56 union all
select 3,35 union all
select 4,6
Go
Select [customer],
sum([amount])总额,
ltrim(cast(sum([amount])*100.0/(select sum([amount])from tb) as dec(18,2)))+'%'百分比
from tb
group by [customer]
/*
customer 总额 百分比
----------- ----------- ------------------------------------------
1 50 29.41%
2 23 13.53%
3 91 53.53%
4 6 3.53%
*/
百分比=ltrim(cast(总额*100.0/(select sum(amount) from tb) as decimal(10,1)))+'%'
from tb
group by 客户