免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8718 | 回复: 5
打印 上一主题 下一主题

mysql case when 统计问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-25 10:23 |只看该作者 |倒序浏览
这是我的sql 语句:

SELECT job_addrc,date_format(job_time,'%Y-%m-%d') as regdate,
sum(case job_status when 50 then 1 else 0 end) as us50,
sum(case job_status when 60 then 1 else 0 end) as us60,
sum(case job_status when 70 then 1 else 0 end) as us70,
sum(case job_status when 80 then 1 else 0 end) as us80
FROM job_info where   job_time between '2001-1-22' and '2009-2-25'
group by regdate,job_addrc order by regdate desc ;



得到的结果是这样的:
job_addrc    regdate        us50    us60    us70      us80
'530',       '2009-02-23',     0,       0,          3,          0
'531',       '2009-02-23',      0,      0,          0,          0
'538',       '2009-02-23',      0,      0,          0,          0
'565',       '2009-02-23',      0,      0,          0,          0
'576',       '2009-02-23',      0,      0,          0,          0
'593',       '2009-02-23',      0,      0,          0,          0
'600',       '2009-02-23',      0,      0,          1,          0
'635',       '2009-02-23',      0,      0,          3,          0
'639',       '2009-02-23',      0,      0,          0,          0
'652',       '2009-02-23',      0,      0,          0,          0
'653',       '2009-02-23',      0,      0,          0,          0
'654',       '2009-02-23',      0,      0,          1,          0
'664',       '2009-02-23',      0,      0,          0,          0
'690',       '2009-02-23',      0,      0,          0,          0
'702',       '2009-02-23',      1,      0,          0,          0
'703',       '2009-02-23',      0,      0,          1,          0

我想要的结果是 us50    us60    us70      us80 这四个的不同时为0的数据
'531',       '2009-02-23',      0,      0,          0,          0
'538',       '2009-02-23',      0,      0,          0,          0
'565',       '2009-02-23',      0,      0,          0,          0
'576',       '2009-02-23',      0,      0,          0,          0


这样的数据是我不要的,在sql语句中我应该怎么过滤掉他们呢。

在线等啊。

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
2 [报告]
发表于 2009-02-25 10:33 |只看该作者
select * from
(

SELECT job_addrc,date_format(job_time,'%Y-%m-%d') as regdate,
sum(case job_status when 50 then 1 else 0 end) as us50,
sum(case job_status when 60 then 1 else 0 end) as us60,
sum(case job_status when 70 then 1 else 0 end) as us70,
sum(case job_status when 80 then 1 else 0 end) as us80
FROM job_info where   job_time between '2001-1-22' and '2009-2-25'
group by regdate,job_addrc order by regdate desc )

as a



where a.us50 !=0 and    a.us60  and !=0  and  a.us70  !=0  and   a.us80!=0
;


简单的就当这个结果一个表再过滤一下。

[ 本帖最后由 枫影谁用了 于 2009-2-25 10:40 编辑 ]

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
3 [报告]
发表于 2009-02-25 10:35 |只看该作者
在原始SQL改下也可以。。group by 前加上where job_status 不等于不在你case的值

论坛徽章:
0
4 [报告]
发表于 2009-02-25 10:39 |只看该作者

回复 #2 枫影谁用了 的帖子

不行啊。
说有语法错误。
我直接粘贴的你sql 。

请再赐教哦

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
5 [报告]
发表于 2009-02-25 10:40 |只看该作者
原帖由 cxr1217 于 2009-2-25 10:39 发表
不行啊。
说有语法错误。
我直接粘贴的你sql 。

请再赐教哦


括号放错位置了 ..

论坛徽章:
0
6 [报告]
发表于 2009-02-25 10:45 |只看该作者

回复 #5 枫影谁用了 的帖子

枫影谁用了
你太可爱了。
谢谢你了。
非常感谢。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP