Group By Count不能显示0的问题
问题:
如对表:
/*====================================================
id |score |grade
---------------------------------------------------
1 |0 |1
2 |4 |1
3 |6 |2
4 |1 |2
5 |7 |3
6 |3 |3
====================================================*/
希望统计各grade中score>5的数量
如果用如下语句:
select grade,count(*) from tmpTable where score>5 group by grade
则不能得到grade==1的结果,但实际是期望得到0的
分析:
造成这一现象的原因是, score<=5的情况都被首先剔除了,无法被group by
解决:
使用如下语句查询:
select a.grade,ifnull(count(*),)
from
(select * from tmpTable group by grade) a
left join
( select grade,count(*) from tmpTable where score> group by grade) b
on a.grade=b.grade
原理:
借助另一个所期待行存在的“表”,通过左联,将期望的行select出来,再借助ifnull函数进行替换 以实现显示0
注意:不同数据库实现ifnull功能可能采用不同的函数,如NVL等。本例使用的数据库是sqlite,其他数据库未测试
Group By Count不能显示0的问题的更多相关文章
- 及格率 不谢 cast(cast (sum(case when res>=60 then 1 else 0 end)*100/(count(1)*1.0) as float) as nvarchar)+'%' '及格率'
--18.查询各科成绩最高分.最低分和平均分:--以如下形式显示:-- 课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率--及格为>=60,中等为:70-80,优良 ...
- MongoDB学习笔记——聚合操作之group,distinct,count
单独的聚合命令(group,distinct,count) 单独聚合命令 比aggregate性能低,比Map-reduce灵活度低:但是可以节省几行javascript代码,后面那句话我自己加的,哈 ...
- org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actua ...
- CAML query for Group by count and data
CAML query for Group by count and data Company Category Product Name Microsoft Developer Visual Stud ...
- 动态IP无法获取默认网关,显示0.0.0.0的解决办法
IP地址使用自动获取IP方式,可以获取到IP地址和子网掩码,默认网关无法获取,显示0.0.0.0,通过修复Winsock和LSP可以解决该问题,具体步骤如下:一.修复winsock1.单击开始> ...
- 20.org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actua ...
- Batch update returned unexpected row count from update [0] 异常处理
在one-to-many时遇到此异常,本以为是配置出错.在使用s标签开启debug模式,并在struts2主配置文件中添加异常映射,再次提交表单后得到以下异常详情. org.springframewo ...
- 关于Hibernate级联更新插入信息时提示主键不为空的问题“org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1 ”
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual ...
- 关于Error during managed flush [Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1]错误
控制台报错: 08:07:09.293 [http-bio-8080-exec-2] ERROR org.hibernate.internal.SessionImpl - HHH000346: Err ...
随机推荐
- 拓扑图弹力布局呈现Flickr图片搜索结果
十年前有值得分享的图片我都存在Flickr上,可惜yahoo收购了Flickr之后堕落好多年,最近yahoo在梅姐带领下Flickr团队终于恢复了生机,个人免费存储空间扩充到了1T,界面用户体验也有 ...
- (8)分布式下的爬虫Scrapy应该如何做-图片下载(源码放送)
转载主注明出处:http://www.cnblogs.com/codefish/p/4968260.html 在爬虫中,我们遇到比较多需求就是文件下载以及图片下载,在其它的语言或者框架中,我们可能 ...
- mysqldump导出部分数据的方法: 加入--where参数
mysqldump导出部分数据的方法: 加入--where参数 mysqldump -u用户名 -p密码 数据库名 表名 --where="筛选条件" > 导出文件路径 my ...
- 控制器中的Action方法,接收浏览器传过来的参数,总共有几种?
1.根据配置文件中的URL规则 public ActionResult Delete(int id) //id参数就是根据路由里面的参数id来传过来的,这个action方法中的参数一定要和路由中的id ...
- 关于PS激活的一些感想(附上PS CC2015)
最近跟着慕课学了一些前端的必备PS技能,就顺道把PS CC2015装上. 安装的过程没什么大问题,最要命的是激活环节!各种踩坑,但万万没想到,最终我还是成功的把它激活了. 本人所安装PS版本信息 好了 ...
- 【C#】属性(Attribute)
如果程序员是猫,你是哪只猫? 这个是我一直都很喜欢的一个技术,不是很麻烦,也不是很难理解,和反射配合起来,只有你想不到没有做不到的用途(夸张了哈). 运用范围 程序集,模块,类型(类,结构,枚举,接口 ...
- 一个Linq表达式的扩展函数帮助类
/// <summary> /// Linq表达式的扩展函数 /// </summary> public static class ExpressionExtensions { ...
- MYSQL企业常用架构与调优经验分享
一.选择Percona Server.MariaDB还是MYSQL mysql应用源码:http://www.jinhusns.com/Products/Download/?type=xcj 1.M ...
- [moka同学笔记]yii表单dropdownlist样式
<?= $form->field($modelUser,'name') ?> <div class="form-group field-community-mobil ...
- .NET Core添加项目之间的依赖关系