本帖最后由 mmc1206x 于 2014-02-19 02:49:41 编辑

解决方案 »

  1.   

    select 部门,sum(case when 性别=男 then 1 else 0 end) ' 男性人数',
                sum(case when 性别=女 then 1 else 0 end) ' 女性人数'
    from MyTable group by 部门
      

  2.   

    试试这个:
    create table  MyTable(部门 varchar(10),  性别 varchar(10))insert into MyTable
    select '部门1',  '男' union all
    select '部门2',  '女' union all
    select '部门3',  '男' union all
    select '部门1',  '男' union all
    select '部门3',  '女'
    goselect 部门,
           count(case when 性别='男' then 1 else null end) ' 男性人数',
           count(case when 性别='女' then 1 else null end) ' 女性人数'
    from MyTable 
    group by 部门
    /*
    部门  男性人数  女性人数
    部门1 2 0
    部门2 0 1
    部门3 1 1
    */
      

  3.   

    select 部门,sum(case when 性别=男 then 1 else 0 end) ' 男性人数',
                sum(case when 性别=女 then 1 else 0 end) ' 女性人数'
    from MyTable group by 部门
      

  4.   

    select 部门,
    sum ( case 性别 when '男'then 1 else 0 end) as '男性人数',
    sum ( case 性别 when '女'then 1 else 0 end) as '女性人数'from mytable group by 部门