34,590
社区成员
发帖
与我相关
我的任务
分享
create table table1(name varchar(10), amount int)
insert into table1
select 'aa',10
union all
select 'bb',20
union all
select 'aa',10
union all
select 'bb',20
union all
select 'cc',30
select name,sum(amount),
left(cast(round(sum(amount)*1.0/(select sum(amount) from table1),2)*100 as varchar(20)),2)+'%'
from table1
group by name
drop table table1
declare @table table(name varchar(10), amount int)
insert into @table
select 'aa',10
union all
select 'bb',20
union all
select 'aa',10
union all
select 'bb',20
union all
select 'cc',30
select name,sum(amount),
left(cast(round(sum(amount)*1.0/(select sum(amount) from @table),2)*100 as varchar(20)),2)+'%'
from @table
group by name
create table table1(name varchar(10),amount int)
insert into table1 values('aa',10)
insert into table1 values('bb',20)
insert into table1 values('aa',10)
insert into table1 values('bb',20)
insert into table1 values('cc',30)
declare @allsum int
select @allsum=sum(amount) from table1
print @allsum
select name,[sum(amount)]=sum(amount),[amount(%)]=substring(convert(varchar(15),0.5+100*(select sum(amount) from table1 where name=a.name)/(cast (@allsum as float))),0,3)+'%' from table1 a group by name
drop table table1
declare @sum float
select @sum = sum(amout) from table1
print @sum
select name, sum(amout),convert(varchar(5),round(sum(amout)/@sum*100,2))+'%'
from table1
group by name;
create table table1(name varchar(10),amount int)
insert into table1 values('aa',10)
insert into table1 values('bb',20)
insert into table1 values('aa',10)
insert into table1 values('bb',20)
insert into table1 values('cc',30)
select name,[sum(amount)]=sum(amount),[amount(%)]=convert(varchar(2),100*(select sum(amount) from table1 where name=a.name)/(select sum(amount) from table1))+'%' from table1 a group by name
name sum(amount) amount(%)
---------- ----------- ---------
aa 20 22%
bb 40 44%
cc 30 33%
(3 行受影响)
declare @tb table (name varchar(10),amount int)
insert into @tb select 'aa',10
insert into @tb select 'bb',20
insert into @tb select 'aa',10
insert into @tb select 'bb',20
insert into @tb select 'cc',30
select name, sum(amount) as s
,ltrim(cast(round(sum(amount)*100.0/(select sum(amount) from @tb),0) as int))+'%' as b
from @tb
group by name