人员情况表(emp)中的字段包括员工号(id)、姓名(name)、年龄(age)、学历(wh),其中学历包括4种情况(本科以上、大专、高中、初中以下),现在要统计出表中学历为本科以上、大专、高中、初中以下,各有多少人,平均年龄、占总人数的百分比。这条语句怎么写啊!!!就T-SQL语句

解决方案 »

  1.   

    select  wh,
    人数=sum([id]),
    平均年龄=avg(age),
    百分比=cast(sum([id])*100/(select sum(id) from emp) as varchar(10))+'%'
    from emp group by wh
      

  2.   

    select a.wh,count(b.*) as howmany,avg(b.age) as avgold,count(b.*)/(select count(1) from emp) as p
    from (
    select distinct wh from emp
    ) a left join emp b on a.wh=b.wh
      

  3.   

    select a.wh,count(b.*) as howmany,avg(b.age) as avgold,count(b.*)/(select count(1) from emp) as p
    from (
    select distinct wh from emp
    ) a left join emp b on a.wh=b.wh
    group by a.wh