首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL多条件分组有关问题,前辈

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

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

热点排行
Bad Request.