select jtbh,sex,hf,sf,
case when  aa.hf='已婚' and aa.sex='女'  and aa.sf='职工' then (select count(*) from 表 where jtbh=aa.jtbh and sex='男' and sf='子女') end 男孩数,
case when  aa.hf='已婚' and aa.sex='女'   and aa.sf='职工' then (select count(*) from 表 where jtbh=aa.jtbh and sex='女' and sf='子女') end 女孩数
from 表 aa

解决方案 »

  1.   

    select jtbh,sex,hf,sf,
    case when  aa.hf='已婚' and aa.sex='女'  and aa.sf='职工' then (select count(*) from 表 where jtbh=aa.jtbh and sex='男' and sf='子女') end 男孩数,
    case when  aa.hf='已婚' and aa.sex='女'   and aa.sf='职工' then (select count(*) from 表 where jtbh=aa.jtbh and sex='女' and sf='子女') end 女孩数
    from 表 aa
      

  2.   

    我的意思是:
    当符合的记录超过一条时,这样的case子查询是不允许的,有没有别的办法??!!