码迷,mamicode.com
首页 > 其他好文 > 详细

使用count结合nvl函数时碰到的问题

时间:2016-04-05 19:50:24      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

count()函数功能:统计表中中某个字段或所有记录个数,字段值为null的不做统计。

手册中解释:

COUNT returns the number of rows returned by the query. You can use it as an aggregate or analytic function.

If you specify DISTINCT, then you can specify only the query_partition_clause of the analytic_clause. The order_by_clause and windowing_clause are not allowed.

If you specify expr, then COUNT returns the number of rows where expr is not null. You can count either all rows, or only distinct values of expr.

If you specify the asterisk (*), then this function returns all rows, including duplicates and nulls. COUNT never returns null.

 

从手册可以可以看到是count()返回的值为Number,也就是说即使某个表中一条记录没有,返回的是0。而nvl()或者nvl2()函数判断为条件为是否为Null

(函数用法参见:http://www.cnblogs.com/space-place/p/5151913.html),故而进行nvl(count(*),1)会出现应用错误。

 

解决方法:用decode函数。

 

举例:

1.建立一张空表,不插入任何数据。

技术分享

 

2.nvl2与count()结合使用:

技术分享

可见返回了错误信息,表test2中并没有数据却返回了1.原因就是count(id)返回了0,而nvl2()判断为null:

技术分享

3.用decode()函数解决:

技术分享

 

使用count结合nvl函数时碰到的问题

标签:

原文地址:http://www.cnblogs.com/space-place/p/5356218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!