相对总数的百分比?select id,F1,F2,rtrim(cast(F2*100.0/(select sum(F2) from 表) as numeric(5,2)))+'%' from 表
declare @t table(id int,F1 varchar(4),F2 int) insert into @t select 1,'A',5 insert into @t select 2,'B',4 insert into @t select 3,'C',3 insert into @t select 4,'D',8 insert into @t select 5,'E',9 insert into @t select 6,'F',12 insert into @t select 7,'G',7select id,F1,F2,rtrim(cast(F2*100.0/(select sum(F2) from @t) as numeric(5,2)))+'%' [percent] from @t/* id F1 F2 percent ----------- ---- ----------- --------- 1 A 5 10.42% 2 B 4 8.33% 3 C 3 6.25% 4 D 8 16.67% 5 E 9 18.75% 6 F 12 25.00% 7 G 7 14.58% */
Try: select id,F1,cast(f2*0.1/(select sum(f2) from 表)*100 as varchar)+'%' from 表
insert into @t select 1,'A',5
insert into @t select 2,'B',4
insert into @t select 3,'C',3
insert into @t select 4,'D',8
insert into @t select 5,'E',9
insert into @t select 6,'F',12
insert into @t select 7,'G',7select id,F1,F2,rtrim(cast(F2*100.0/(select sum(F2) from @t) as numeric(5,2)))+'%' [percent] from @t/*
id F1 F2 percent
----------- ---- ----------- ---------
1 A 5 10.42%
2 B 4 8.33%
3 C 3 6.25%
4 D 8 16.67%
5 E 9 18.75%
6 F 12 25.00%
7 G 7 14.58%
*/
select id,F1,cast(f2*0.1/(select sum(f2) from 表)*100 as varchar)+'%'
from 表