如何在 Oracle/PLSQL 中只计算 NULL 值?
如何在 Oracle/PLSQL 中只计算 NULL 值?
我只想计算空值。有没有一个函数可以做到这一点?
How can I count only NULL values in Oracle/PLSQL?
I want to count only the null values. Is there a function that does that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我具体不了解 Oracle,但 ANSI SQL,
COUNT(rowName)
不会计算NULL
值,但COUNT(*)
会计算。因此,您可以编写计算 YourTable 中将 YourColumn 设置为 NULL 的行的数量。
I don't know Oracle specifally, but ANSI SQL,
COUNT(rowName)
does not countNULL
values, butCOUNT(*)
does. So you can writewhich counts the rows in YourTable that have YourColumn set to NULL.
作为 mdma 响应的替代方案。
如果您不想在可以的地方放置过滤器
As an alternative to mdma's response.
If you don't want to put a filter in the where you can
Oracle 文档指出:
例如,使用 scott 模式:
您可以看到 Comm 列有 4 个已知值(即 Not null)和 10 个未知值(即 Null),
因为
count(your_column_name)
会忽略您需要的 null将未知值替换为您可以参考的值。这可以使用 NVL 函数来实现。我使用值“-1”作为空值的“别名”,因为我知道“-1”不是 comm 列中的现有值。
编辑:
遵循罗布的建议。可以从上面的示例中删除 where 子句并使用 NVL2功能如下图:
The Oracle documentation states that:
As an example, using the scott schema:
You can see that the Comm column has 4 known values (i.e. Not null) and 10 unknown values (i.e. Null)
As
count(your_column_name)
ignores nulls you need to substitute the unknown values for something you can refer to. This can be achieved using the NVL function.I have used the value "-1" as the "alias" for my null values because I know that "-1" is not an existing value within the comm column.
EDIT:
Following Rob's suggestion. It is possible to remove the where clause from the above example and use the NVL2 function as shown below:
如果您也想使用 null 来计算其他值,则使用 COALESCE 函数将缩短执行时间
Oracle NVL 和 Coalesce 之间的差异
If you wants to count other values too with null then use of COALESCE function will improves execution time
Oracle Differences between NVL and Coalesce
我可能会尝试反转 null,查看结果
一切都应该等于 nulls + notnulls
I might try to inverse the null, see results
Everything should equal nulls + notnulls
功能:
用途:
Function:
Usage:
我相信您的要求如下:
表
emp
有 100 行。对于 20 名员工,HIRE_DATE
列为NULL
。所以基本上,你想要得到 20 作为输出。这是除了本论坛其他贡献者给出的答案之外的另一种方法。
I believe your requirement is as below:
Table
emp
has 100 rows. Against 20 employees,HIRE_DATE
column isNULL
. So basically, you want to get 20 as output.This is another method apart from the answers given by other contributors in this forum.