--分组列出性别 计算男女各人数 和所占比例
select tsex ,count(*) from person_info where ttype='在职人员' group by tsex
--总人数
select count(*) from person_info
--占比
select cast( convert (decimal(18,2),100*cast(count(*) as float)/cast((select count(*) from person_info) as float) ) as varchar)+'%' from person_info
--每个占比
select tsex 性别 ,count(*) 人数,
cast( convert (decimal(18,2),100*cast(count(*) as float)/cast((select count(*) from person_info) as float) ) as varchar)+'%'
from person_info where ttype='在职人员' group by tsex
--存储过程
alter proc proc_person_sex_select
as
begin
declare @sumPerson float
set @sumPerson=cast((select count(*) from person_info) as float)
select tsex 性别 ,count(*) 人数,
cast( convert (decimal(18,2),100*cast(count(*) as float)/@sumPerson ) as varchar)+'%' 各占比例
from person_info where ttype='在职人员' group by tsex
end
--执行
exec proc_person_sex_select