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 ...
随机推荐
- sqlserver查找表在哪个数据库脚本
EXEC sp_MSforeachdb @command1='IF object_id(''?'' + ''..table_name'') IS NOT NULL PRINT ''?'''
- SQL Server 2014,改善的临时表缓存
在一些先决条件下,SQL Server可以缓存临时表(cache Temp Tables).缓存临时表意味着当你创建反复创建同个临时表时,SQL Server就可以重用它们.这会从整体上大幅度提高你的 ...
- Spring基础——在 IOC 容器中 Bean 之间的关系
一.在 Spring IOC 容器中 Bean 之间存在继承和依赖关系. 需要注意的是,这个继承和依赖指的是 bean 的配置之间的关系,而不是指实际意义上类与类之间的继承与依赖,它们不是一个概念. ...
- Gradle学习系列之八——构建多个Project
在本系列的上篇文章中,我们讲到了Gradle的依赖管理,在本篇文章中,我们将讲到如何构建多个Project. 请通过以下方式下载本系列文章的Github示例代码: git clone https:// ...
- Entity FrameWork 增删查改
Add #region 1.0 新增+void Add() /// <summary> /// 新增 /// </summary> static void Add() { // ...
- .Net配置文件——反射+配置文件存储类型实例
配置文件+反射确实去除了选择语句的繁琐,带来了优美的赶脚! 首先改进了一下类(接上文): ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
- C#获取url中参数键值对的方法
方法如下: /// <summary> /// 遍历Url中的参数列表 /// </summary> /// <returns>如:(?userName=keley ...
- Microsecond and Millisecond C# Timer[转]
文章转至:http://www.codeproject.com/Articles/98346/Microsecond-and-Millisecond-NET-Timer IntroductionAny ...
- sql 去重
;WITH CETAS (SELECT *, ROW_NUMBER() OVER (PARTITION BY SearchTask_PKID ORDER BY SearchTask_PKID) Row ...
- php中的字符串常用函数(一) strpos() 子字符首次出现的位置
strpos($str, $needle); 1.返回$needle在$str首次出现的位置.(大小写敏感). 2.从php5开始$needle支持多字符.php4只能用单个字符. 3.能找到$nee ...