oracle sum count 的区别
来源:互联网 发布:matlab中两个矩阵相减 编辑:程序博客网 时间:2024/05/17 18:44
转载于:http://dev.firnow.com/course/7_databases/oracle/oraclejs/20071211/91840.html
在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询。比如aaa表:
ID,NAME,SHU
1,a,3
2,a,4
3,b,5
4,b,7
5,a,
6,a,20
例1:查询出现过2次的name。
往往初学者会错误地认为在where 语句里直接使用count()算法,很显然这个想法是错误的,count()方法并不能被用在where子句中,为了解决问题,我们可以在group by子句后面使用HAVING来做条件限制。
错误做法:select * from aaa where count(name)>=2 group by name;
正确做法:select * from aaa group by name HAVING count(name)>=2 ;
解释说明:HAVING 与 WHERE 类似,可用来决定选择哪些记录。HAVING 子句在SELECT语句中指定,显示哪些已用 GROUP BY 子句分组的记录。在GROUP BY组合了记录后, HAVING会显示 GROUP BY 子句分组的任何符合 HAVING 子句的记录。
例2:查询单一用户的SHU总和大于10的用户。
有前面的经验,把sum()方法写在HAVING子句中。
正确做法:select * from aaa group by name HAVING sum(SHU)>10 ;
注意:一个HAVING子句最多只能包含40个表达式,HAVING子句的表达式之间可以用AND和OR分割。
例3:sum 与count 的区别
select name, count(shu), sum(shu)
from aaa
group by name
结果为:a 3 27
b 2 12
可见:oracle 数据库中对空值的事不同的,count 函数认为没有此项,而sum函数不计算。
结论: 求和用累加 sum ,求行的个数用累计 count
文章出处:飞诺网(www.firnow.com):http://dev.firnow.com/course/7_databases/oracle/oraclejs/20071211/91840.html
- oracle sum count 的区别
- oracle sum count 的区别
- oracle 中sum和count的区别
- oracle 中sum和count的区别
- sql中sum()和count()的区别
- sql中sum()和count()的区别
- Mysql中sum()与count()的区别
- 数据库中sum和count的区别
- sql中sum()和count()的区别
- sql中sum()和count()的区别
- Oracle 中count(1) 和count(*) 的区别
- Oracle 中count(1) 和count(*) 的区别
- Oracle ——count(*) 与count(列名)的区别
- Oracle 中count(1) 和count(*) 的区别
- Oracle 中count(1) 和count(*) 的区别
- Oracle 中count(1) 和count(*) 的区别
- Oracle 中count(1) 和count(*) 的区别
- Oracle 中count(1) 和count(*) 的区别
- AfxMessageBox比MessageBox区别
- assert函数的作用
- TRACE()语句
- python 多线程下载
- oracle时间处理函数
- oracle sum count 的区别
- 函数框架
- Oracle数据库Decode()函数的使用方法
- web.config加密解密
- CSocket 和CAsyncSocket类介绍
- Oracle SQL精妙SQL语句讲解
- cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- linux vsftp 设置
- 没写完