select  wh as 学历,age as 年龄,count(id) as 人数,count(id)/(select sum(id) from tb group by age) as 百分比 from tb  
  group by wh,age;

解决方案 »

  1.   

    select 学历,年龄,count(*) as 人数,count(*)/(select sum(人数) from table group by 年龄) as 百分比
    from table
    group by 学历,年龄
      

  2.   

    create table tb(id int, name varchar(10), age int, wh varchar(10))
    insert into tb values(1 , 'aa' , '21' , '高中') 
    insert into tb values(2 , 'ab' , '21' , '大专') 
    insert into tb values(3 , 'ac' , '22' , '高中') 
    insert into tb values(4 , 'ad' , '22' , '大专') 
    insert into tb values(5 , 'ae' , '22' , '大专') 
    insert into tb values(6 , 'af' , '22' , '本科') 
    insert into tb values(7 , 'ag' , '23' , '高中') 
    insert into tb values(8 , 'ah' , '23' , '本科') 
    insert into tb values(9 , 'aj' , '23' , '本科') 
    insert into tb values(10, 'ak' , '24' , '大专') 
    insert into tb values(11, 'al' , '24' , '大专') 
    insert into tb values(12, 'ap' , '24' , '本科') 
    goselect wh 学历, age 年龄, count(*) 人数, sum(百分比) 百分比 from
    (
      select wh , age , 100/(select count(*) from tb where age = t.age) 百分比 from tb t 
    ) m
    group by  wh , agedrop table tb/*
    学历         年龄          人数          百分比         
    ---------- ----------- ----------- ----------- 
    大专         21          1           50
    高中         21          1           50
    本科         22          1           25
    大专         22          2           50
    高中         22          1           25
    本科         23          2           66
    高中         23          1           33
    本科         24          1           33
    大专         24          2           66(所影响的行数为 9 行)
    */
      

  3.   


    --方法就这样,数据自己添加
    declare @t table
    (
    id int identity(1,1),
    name char(2),
    age int,
    wh varchar(4)
    )insert @t select 'aa',21,'高中'
    union all select 'ab',21,'大专'
    union all select 'ac',22,'高中'
    union all select 'ad',22,'大专'
    union all select 'ae',22,'大专'
    select
      a.wh,
      a.age,
      count(*) as 人数,
      百分比 = floor(count(*) / ((
    select count(*)
    from @t
    where age = a.age
    ) * 1.0) * 100)
    from @t a
    group by a.wh,a.age
    order by a.age,a.wh desc
    /**
    高中 21 1 50
    大专 21 1 50
    高中 22 1 33
    大专 22 2 66
    **/
      

  4.   

    create table tb(id int, name varchar(10), age int, wh varchar(10))
    insert into tb values(1 , 'aa' , '21' , '高中') 
    insert into tb values(2 , 'ab' , '21' , '大专') 
    insert into tb values(3 , 'ac' , '22' , '高中') 
    insert into tb values(4 , 'ad' , '22' , '大专') 
    insert into tb values(5 , 'ae' , '22' , '大专') 
    insert into tb values(6 , 'af' , '22' , '本科') 
    insert into tb values(7 , 'ag' , '23' , '高中') 
    insert into tb values(8 , 'ah' , '23' , '本科') 
    insert into tb values(9 , 'aj' , '23' , '本科') 
    insert into tb values(10, 'ak' , '24' , '大专') 
    insert into tb values(11, 'al' , '24' , '大专') 
    insert into tb values(12, 'ap' , '24' , '本科') 
    go
    select wh , age , count(*) 人数, count(*)*100/(select count(*) from tb where age = t.age) 百分比 from tb t group by  wh , agedrop table tb/*
    wh         age         人数          百分比         
    ---------- ----------- ----------- ----------- 
    大专         21          1           50
    高中         21          1           50
    本科         22          1           25
    大专         22          2           50
    高中         22          1           25
    本科         23          2           66
    高中         23          1           33
    本科         24          1           33
    大专         24          2           66(所影响的行数为 9 行)
    */
      

  5.   

    create table tb(id int, name varchar(10), age int, wh varchar(10))
    insert into tb values(1 , 'aa' , '21' , '高中') 
    insert into tb values(2 , 'ab' , '21' , '大专') 
    insert into tb values(3 , 'ac' , '22' , '高中') 
    insert into tb values(4 , 'ad' , '22' , '大专') 
    insert into tb values(5 , 'ae' , '22' , '大专') 
    insert into tb values(6 , 'af' , '22' , '本科') 
    insert into tb values(7 , 'ag' , '23' , '高中') 
    insert into tb values(8 , 'ah' , '23' , '本科') 
    insert into tb values(9 , 'aj' , '23' , '本科') 
    insert into tb values(10, 'ak' , '24' , '大专') 
    insert into tb values(11, 'al' , '24' , '大专') 
    insert into tb values(12, 'ap' , '24' , '本科') 
    go
    select wh , age , count(*) 人数, cast(count(*)*100.0/(select count(*) from tb where age = t.age) as decimal(18,2)) 百分比 from tb t group by  wh , agedrop table tb/*
    wh         age         人数          百分比                  
    ---------- ----------- ----------- -------------------- 
    大专         21          1           50.00
    高中         21          1           50.00
    本科         22          1           25.00
    大专         22          2           50.00
    高中         22          1           25.00
    本科         23          2           66.67
    高中         23          1           33.33
    本科         24          1           33.33
    大专         24          2           66.67(所影响的行数为 9 行)
    */