select sex
      ,sum(case when sex='男' then 1 else 0 end) 男性个数
       ,sum(case when age>20 then 1 else 0 end) [大于20岁的个数]
from 表A
group by sex

解决方案 »

  1.   

    create table tb(id int,name varchar(20),sex varchar(20),age int)
    insert tb select 1 ,      '张三'  ,'男' ,      30 
    insert tb select 2  ,     '李四'  , '男' ,      25 
    insert tb select 3   ,    '小梅'  , '女'  ,     18 
    insert tb select 4    ,   '大明'  , '男'   ,    40 
    insert tb select 5     ,  '大时'  , '女'    ,   30 select sex
          ,count(*) 男性个数
           ,sum(case when age>20 then 1 else 0 end) [大于20岁的个数]
    from tb
    where sex='男'
    group by sex
    drop table tb/*
    sex                  男性个数        大于20岁的个数    
    -------------------- ----------- ----------- 
    男                    3           3(所影响的行数为 1 行)
    */
      

  2.   

    create table tb(id int,name varchar(20),sex varchar(20),age int)
    insert tb select 1 ,      '张三'  ,'男' ,      30 
    insert tb select 2  ,     '李四'  , '男' ,      25 
    insert tb select 3   ,    '小梅'  , '女'  ,     18 
    insert tb select 4    ,   '大明'  , '男'   ,    40 
    insert tb select 5     ,  '大时'  , '女'    ,   30 select sex
          ,count(*) 男性个数
           ,sum(case when age>20 then 1 else 0 end) [大于20岁的个数]
    from tbgroup by sex
    drop table tb/*
    sex                  男性个数        大于20岁的个数    
    -------------------- ----------- ----------- 
    男                    3           3
    女                    2           1(所影响的行数为 2 行)
    */
      

  3.   

    解决了,请结帖结帖方式:管理帖子->给分->输入密码->结帖谢谢.
      

  4.   


    select 男人数=sum(case sex when '男' then 1 else 0 end),年龄大于20的人数=sum(case  when age>=20 then 1 else 0 end) from tb男人数         年龄大于20的人数
    ----------- -----------
    3           4(1 行受影响)