问题:

  如对表:

/*====================================================
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的问题的更多相关文章

  1. 及格率 不谢 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,优良 ...

  2. MongoDB学习笔记——聚合操作之group,distinct,count

    单独的聚合命令(group,distinct,count) 单独聚合命令 比aggregate性能低,比Map-reduce灵活度低:但是可以节省几行javascript代码,后面那句话我自己加的,哈 ...

  3. 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 ...

  4. CAML query for Group by count and data

    CAML query for Group by count and data Company Category Product Name Microsoft Developer Visual Stud ...

  5. 动态IP无法获取默认网关,显示0.0.0.0的解决办法

    IP地址使用自动获取IP方式,可以获取到IP地址和子网掩码,默认网关无法获取,显示0.0.0.0,通过修复Winsock和LSP可以解决该问题,具体步骤如下:一.修复winsock1.单击开始> ...

  6. 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 ...

  7. Batch update returned unexpected row count from update [0] 异常处理

    在one-to-many时遇到此异常,本以为是配置出错.在使用s标签开启debug模式,并在struts2主配置文件中添加异常映射,再次提交表单后得到以下异常详情. org.springframewo ...

  8. 关于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 ...

  9. 关于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 ...

随机推荐

  1. 使用Eclipse调试PHP程序

    我安装的是PHP Version 5.3.26,按照网上提示在Eclipse中使用XDebug进行调试,不过配置了却使用不了,下面把解决方法简要说一下. XDebug老是加载不了 From PHP 5 ...

  2. Hekaton是如何影响你数据库的目标恢复时间(RTO)的

    这个周末我发现了SQL Server 2014里Hekaton的一个有趣副作用,很遗憾它会负面影响你数据库的目标恢复时间(Recovery Time Objective,RTO).你已知道,对于每个本 ...

  3. ASP.NET身份验证

    Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验 证用的最多,也最灵活. Forms 验证方式对基于用户的验证 ...

  4. Javascript语法去控制Web控件的Enabled属性

    Web控件当使用Enabled属性时,它生成html之后会变成了disabled了.我们为了能够在javascript去控制控件的禁用与启用,得从这个disabled入手.如:

  5. C# FTP远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件)

    今天用代码删除FTP服务器上的目录时候,报错:远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件). 习惯性的google,不外乎以下几点: 1.URL路径不对,看看有没有多加 ...

  6. HipChat上传文件报未知错误解决方案

    前言 HipChat是Atlassian公司的一款团队协作即时通讯工具,服务端为Linux(官方给的服务端就是一个虚拟机),在Windows.Linux.Android.IOS.Mac等平台都有客户端 ...

  7. 学习笔记(一)——MVC扩展

    1.视图引擎的作用,总结为两点: 查找视图 渲染视图 ViewEngine即视图引擎, 在ASP.NET MVC中将ViewEngine的作用抽象成了 IViewEngine 接口. 默认情况下,AS ...

  8. 重新想象 Windows 8 Store Apps (67) - 后台任务: 推送通知

    [源码下载] 重新想象 Windows 8 Store Apps (67) - 后台任务: 推送通知 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台任务 推送通 ...

  9. javascript: detect mobile devices or browser

    http://detectmobilebrowsers.com/ http://hgoebl.github.io/mobile-detect.js/ http://www.hand-interacti ...

  10. 重载赋值运算符 && 对象

    class CMessage { private: char * m_pMessage; public: void showIt()const { cout << m_pMessage & ...