select 学历程度,所占人数,所占人数*100/(select count(*) from 表) from ( select '本科学历' as 学历程度,IsNull((select count(*) from 表),0) as 所占人数 from 表 where 学历程度 = '本科' union all select '大专学历' as 学历程度,IsNull((select count(*) from 表),0) as 所占人数 from 表 where 学历程度 = '大专' union all select '中专学历' as 学历程度,IsNull((select count(*) from 表),0) as 所占人数 from 表 where 学历程度 = '中专' ) tmp
select a.学历名称, isnull(b.所占人数, 0) as 所占人数, isnull(b.所占比例, 0) as 所占比例 from (select '本科' as 学历名称 union all select '大专' union all select '中专') a left outer join (select 学历程度, count(*) as 所占人数, count(*) * 100 / (select count(*) from 表) as 所占比例 group by 学历程度) b on a.学历名称 = b.学历程度
select 学历程度, count(*) as 所占人数 from tablename group by 学历程度
select 学历名称=学历程度+'学历' ,所占人数=cast(count(*)*100/(select count(*) from 表) as varchar)+'%' from 表 group by 学历程度
select 学历程度,count(*) 所占人数, cast(100*count(*)/(select count(*) from [table]) as varchar(10))+'%' 所占比例 from [table] group by 学历程度
select T.学历, (case when T.人数 is null then 0 else T.人数) as 人数, (case when t.人数 is null then 0 else t.人数/c.总人数) as 比率 from ( 学历表 a left join ( select 学历,sum(*) as 人数 from 员工 b group by 学历 ) b on a.学历=b.学历 ) T , ( select count(*) as 总人数 from 员工 ) c
select 学历程度,sum(1) 所占人数,cast(100*sum(1)/tem.cou as varchar(10))+'%' 所占比例 from [table],(select count(*) cou from [table]) tem group by 学历程度这个效率高
--少了一个东西,改一下: select 学历名称=a.学历程度+'学历' ,所占人数=cast(count(b.编号)*100/(select count(*) from 表) as varchar)+'%' from ( select 学历程度='中专' union all select '大专' union all select '本科' ) a left join 表 b on a.学历程度=b.学历程度 group by a.学历程度
--差了人数,少了一个东西,改一下: select 学历名称=a.学历程度+'学历',所占人数=count(b.编号) ,所占比例=cast(count(b.编号)*100/(select count(*) from 表) as varchar)+'%' from ( select 学历程度='中专' union all select '大专' union all select '本科' ) a left join 表 b on a.学历程度=b.学历程度 group by a.学历程度
--下面是数据测试:--测试数据 declare @表 table(编号 varchar(5),姓名 varchar(10),学历程度 varchar(10)) insert into @表 select '03001','aa','大专' union all select '03002','bb','中专' union all select '03003','cc','中专'--查询 select 学历名称=a.学历程度+'学历',所占人数=count(b.编号) ,所占比例=cast(count(b.编号)*100/(select count(*) from @表) as varchar)+'%' from ( select 学历程度='中专' union all select '大专' union all select '本科' ) a left join @表 b on a.学历程度=b.学历程度 group by a.学历程度/*--测试结果 学历名称 所占人数 所占比例 -------- ----------- ------------------------------- 本科学历 0 0% 大专学历 1 33% 中专学历 2 66%(所影响的行数为 3 行) --*/
select a.学历程度+'学历' 学历名称,count(*) 所占人数,cast(100*count(b.编号)/(select count(*) from 你的表) as varchar(10))+'%' from (select '中专' 学历程度 union select '大专' union select '本科') a left join 你的表 b on a.学历程度=b.学历程度 group by a.学历程度
select a.学历程度+'学历' 学历名称,count(*) 所占人数,cast(100*count(b.编号)/(select count(*) from 你的表) as varchar(10))+'%' from (select '中专' 学历程度 union select '大专' union select '本科') a left join 你的表 b on a.学历程度=b.学历程度 group by a.学历程度
from
(
select '本科学历' as 学历程度,IsNull((select count(*) from 表),0) as 所占人数
from 表 where 学历程度 = '本科'
union all
select '大专学历' as 学历程度,IsNull((select count(*) from 表),0) as 所占人数
from 表 where 学历程度 = '大专'
union all
select '中专学历' as 学历程度,IsNull((select count(*) from 表),0) as 所占人数
from 表 where 学历程度 = '中专'
) tmp
(select '本科' as 学历名称 union all select '大专' union all select '中专') a
left outer join
(select 学历程度, count(*) as 所占人数, count(*) * 100 / (select count(*) from 表) as 所占比例 group by 学历程度) b
on a.学历名称 = b.学历程度
,所占人数=cast(count(*)*100/(select count(*) from 表) as varchar)+'%'
from 表
group by 学历程度
cast(100*count(*)/(select count(*) from [table]) as varchar(10))+'%' 所占比例
from [table]
group by 学历程度
T.学历,
(case when T.人数 is null then 0 else T.人数) as 人数,
(case when t.人数 is null then 0 else t.人数/c.总人数) as 比率
from
( 学历表 a left join
( select 学历,sum(*) as 人数
from 员工 b
group by 学历
) b on a.学历=b.学历
) T ,
( select count(*) as 总人数
from 员工
) c
select 学历名称=a.学历程度+'学历'
,所占人数=cast(count(b.编号)*100/(select count(*) from 表) as varchar)+'%'
from (
select 学历程度='中专' union all select '大专' union all select '本科'
) a left join 表 b on a.学历程度=b.学历程度
group by a.学历程度
select 学历名称=a.学历程度+'学历',所占人数=count(b.编号)
,所占比例=cast(count(b.编号)*100/(select count(*) from 表) as varchar)+'%'
from (
select 学历程度='中专' union all select '大专' union all select '本科'
) a left join 表 b on a.学历程度=b.学历程度
group by a.学历程度
declare @表 table(编号 varchar(5),姓名 varchar(10),学历程度 varchar(10))
insert into @表
select '03001','aa','大专'
union all select '03002','bb','中专'
union all select '03003','cc','中专'--查询
select 学历名称=a.学历程度+'学历',所占人数=count(b.编号)
,所占比例=cast(count(b.编号)*100/(select count(*) from @表) as varchar)+'%'
from (
select 学历程度='中专' union all select '大专' union all select '本科'
) a left join @表 b on a.学历程度=b.学历程度
group by a.学历程度/*--测试结果
学历名称 所占人数 所占比例
-------- ----------- -------------------------------
本科学历 0 0%
大专学历 1 33%
中专学历 2 66%(所影响的行数为 3 行)
--*/