我有一个table,通过查询语句 :
select zj,xb,renshu=count(*) from 体检信息表 group by zj,xb order by zj  已经取得了table中的各种疾病的人数.现在想在此表的基础上,在实现,统计出zj=正常和zj<>正常(所有非'正常'结果都是这类),请问在以上语句基础上怎么嵌套使用.

解决方案 »

  1.   

    --try
    select zj,xb,renshu=count(*),
    正常人数=sum(case when zj='正常' then 1 else 0 end),
    非正常人数=sum(case when zj='非正常' then 1 else 0 end)
    from 体检信息表 
    group by zj,xb 
    order by zj
      

  2.   

    --这样?
    select 
          case when zj='正常' then '正常' else '非正常' end as zj,
          sum(renshu) as renshu
    from 
      (
          select zj,xb,renshu=count(*) 
          from 体检信息表 
          group by zj,xb 
      ) T
    group by case when zj='正常' then '正常' else '非正常' end