[英]Oracle SQL Developer - Count function
This is the output of a select * from table1, I have a doubt with count function... I want to count that NULL, in order to do that the proper option is to do this: 这是来自table1的select *的输出,我对count函数有疑问...我想对那个NULL进行计数,以便执行此操作是正确的选择:
select count(*) from table1 where fecha_devolucion is null --> This gives me the proper answer counting 1 however if i do: 从fecha_devolucion为null的表1中选择count(*)->这给了我正确的答案计数1,但是如果我这样做:
select count(fecha_devolucion)
from table1
where fecha_devolucion is null --> this returns 0, why? Isn't the same syntax?
What's the difference between choosing a specific field and * from a table? 从表中选择特定字段和*有什么区别?
From the documentation ( http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions032.htm ): 从文档( http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions032.htm ):
If you specify expr, then COUNT returns the number of rows where expr is not null.
如果指定expr,则COUNT返回expr不为null的行数。 ...
...
If you specify the asterisk (*), then this function returns all rows...
如果指定星号(*),则此函数返回所有行...
In other words, COUNT(fecha_devolucion) counts non-NULL values of that column. 换句话说,COUNT(fecha_devolucion)计算该列的非空值。 COUNT(*) counts the total number of rows, regardless of the values.
COUNT(*)计算行总数,而不管其值如何。
Let's compare the two queries: 让我们比较两个查询:
select count(*)
from table1
where fecha_devolucion is null;
select count(fecha_devolucion)
from table1
where fecha_devolucion is null;
I think you misunderstand the count()
function. 我认为您误解了
count()
函数。 This function counts the number of non- NULL
values in its argument list. 该函数计算其参数列表中非
NULL
值的数量。 With a constant or *
, it counts all rows. 使用常数或
*
,它将计算所有行。
So, the first counts all the matching rows. 因此,第一个计数所有匹配的行。 The second counts all the non-NULL values of
fecha_devolucion
. 第二个计数
fecha_devolucion
所有非NULL值。 But there are no such values because of the where
clause. 但是由于
where
子句,因此没有此类值。
By the way, you can also do: 顺便说一句,您还可以执行以下操作:
select sum(case fecha_devolucion is null then 1 else 0 end) as Nullfecha_devolucion
from table1;
这是获取计数的另一种方法:
SELECT SUM(NVL(fecha_devolucion,1)) FROM table1 WHERE fecha_devolucion IS NULL;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.