select name, count(1)*100.0/nullif((select count(1) from tab),0) from tab group by name
declare @t table (NAME varchar(1),L其他字段 varchar(3)) insert into @t select 'A','xxx' union all select 'A','xxx' union all select 'B','xxx' union all select 'B','xxx' union all select 'B','xxx'select NAME, 百分比=ltrim(cast(cast(count(*) as decimal(16,2))*100/ (select count(*) from @t) as decimal(18,0)))+'%' from @t group by NAME /* NAME 百分比 ---- ------------------------------------------ A 40% B 60% */
select name,rtrim(count(1)*100./(select count(1) from tb))+'%' from tb group by name
create table #t (aa varchar(10)) insert into #t select 'A' union all select 'A' union all select 'B'union all select 'B'union all select 'B' select aa,t,cast(t as decimal(8,2))/t2 *100 from (select aa,count(*) as t from #t group by aa) a left join (select count(*) as t2 from #t ) b on 1=1
select name,rtrim(count(1)*100./(select count(1) from tb))+'%' as 百分比 from tb group by name
declare @t table (NAME varchar(1),L其他字段 varchar(3))
insert into @t
select 'A','xxx' union all
select 'A','xxx' union all
select 'B','xxx' union all
select 'B','xxx' union all
select 'B','xxx'select NAME,
百分比=ltrim(cast(cast(count(*) as decimal(16,2))*100/
(select count(*) from @t) as decimal(18,0)))+'%'
from @t group by NAME
/*
NAME 百分比
---- ------------------------------------------
A 40%
B 60%
*/
from tb group by name
create table #t (aa varchar(10))
insert into #t
select 'A' union all
select 'A' union all
select 'B'union all
select 'B'union all
select 'B'
select aa,t,cast(t as decimal(8,2))/t2 *100 from
(select aa,count(*) as t from #t group by aa) a left join
(select count(*) as t2 from #t ) b on 1=1
name,rtrim(count(1)*100./(select count(1) from tb))+'%' as 百分比
from
tb
group by
name