SQL多条件分组问题,请教各位前辈。

mg42 2007-06-29 01:27:18
一个表,只有两个字段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

应该怎么写这个多条件分组语句啊?
...全文
387 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jassycn 2010-11-13
  • 打赏
  • 举报
回复
我也遇到类似的问题,看看能不能解决了
mg42 2007-06-29
  • 打赏
  • 举报
回复
多谢弟兄们帮助,终于解决了
Zack999 2007-06-29
  • 打赏
  • 举报
回复
create table test(
title varchar(5),
ifshow bit
)
insert into test
select 'a',1 union all
select 'b',1 union all
select 'a',0 union all
select 'd',1 union all
select 'b',1

select title,count((case when ifshow=1 then 1 end)) isshow_num,count((case when ifshow=0 then 1 end)) noshow_num
from test
group by title

drop table test
yrwx001 2007-06-29
  • 打赏
  • 举报
回复
select a.title,isshow_num = (select count(1) from 表 b where b.title = a.title and a.ifshow = 1),noshow_num = (select count(1) from 表 b where b.title = a.title and a.ifshow = 0) from 表 a
hellowork 2007-06-29
  • 打赏
  • 举报
回复
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
bill024 2007-06-29
  • 打赏
  • 举报
回复
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

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧