select count(*) [总人数] , count(case when 性别 = '女'then 1 end) [女性人数],count(case when 年级 = '一年级' then 1 end) [一年级人数]
from student

解决方案 »

  1.   

    select 总人数=count(*)
      ,女性人数=sum(case 性别 when '女' then 1 else 0 end)
      ,一年级人数=sum(case 年级 when '一年级' then 1 else 0 end)
    from 表
      

  2.   

    --下面是测试
    declare @student table(姓名 varchar(10),性别 varchar(2),年级 varchar(10))
    insert into @student
    select '小刚','男','一年级'
    union all select '小红','女','一年级'
    union all select '小关','男','二年级'
    union all select '小桦','女','二年级'--查询
    select 总人数=count(*)
      ,女性人数=sum(case 性别 when '女' then 1 else 0 end)
      ,一年级人数=sum(case 年级 when '一年级' then 1 else 0 end)
    from @student/*--测试结果总人数         女性人数        一年级人数       
    ----------- ----------- ----------- 
    4           2           2(所影响的行数为 1 行)
    --*/