日期:2014-05-19  浏览次数:20550 次

SQL多条件分组问题,请教各位前辈。
一个表,只有两个字段title、ifshow;结构如下(ifshow为状态)

title         ifshow

a                 1
b                 1
c                 0
d                 1
a                 0
a                 1
c                 1


现在要求,分别统计ifshow=1和ifshow=0两种状态下title字段里的统计值,要求查询生成以下数据集


title         isshow_num         noshow_num
a                 2                             1    
b                 1                             0
c                 1                             1
d                 1                             0

应该怎么写这个多条件分组语句啊?

------解决方案--------------------
select title,
isshow_num=sum(case when ifshow=1 then 1 else 0 end),
noshow_num=sum(case when ifshow=0 then 1 else 0 end)
from 表
group by title
------解决方案--------------------
select title,
isshow_num = sum(case when ifshow = 1 then 1 else 0 end),
noshow_num = sum(case when ifshow = 0 then 1 else 0 end)
from table group by title