如何在 Oracle/PLSQL 中只计算 NULL 值?

发布于 2024-09-01 20:34:14 字数 68 浏览 4 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

相守太难 2024-09-08 20:34:14

我具体不了解 Oracle,但 ANSI SQL,COUNT(rowName) 不会计算 NULL 值,但 COUNT(*) 会计算。因此,您可以编写

SELECT COUNT(*) FROM YourTable WHERE YourColumn IS NULL

计算 YourTable 中将 YourColumn 设置为 NULL 的行的数量。

I don't know Oracle specifally, but ANSI SQL, COUNT(rowName) does not count NULL values, but COUNT(*) does. So you can write

SELECT COUNT(*) FROM YourTable WHERE YourColumn IS NULL

which counts the rows in YourTable that have YourColumn set to NULL.

幸福%小乖 2024-09-08 20:34:14

作为 mdma 响应的替代方案。
如果您不想在可以的地方放置过滤器

SELECT COUNT(case when xxx IS NULL THEN 1 end) cnt_xxx_null
FROM table

As an alternative to mdma's response.
If you don't want to put a filter in the where you can

SELECT COUNT(case when xxx IS NULL THEN 1 end) cnt_xxx_null
FROM table
故事和酒 2024-09-08 20:34:14

Oracle 文档指出:

所有聚合函数,除了
COUNT(*) 和 GROUPING 忽略空值。
您可以使用 NVL 功能
聚合函数的参数
用一个值替换 null。

例如,使用 scott 模式:

SQL> select empno, sal, comm
  2  from emp;

     EMPNO        SAL       COMM
---------- ---------- ----------
      7369        800
      7499       1600        300
      7521       1250        500
      7566       2975
      7654       1250       1400
      7698       2850
      7782       2450
      7788       3000
      7839       5000
      7844       1500          0
      7876       1100
      7900        950
      7902       3000
      7934       1300

14 rows selected.

您可以看到 Comm 列有 4 个已知值(即 Not null)和 10 个未知值(即 Null),

因为 count(your_column_name) 会忽略您需要的 null将未知值替换为您可以参考的值。这可以使用 NVL 函数来实现。

SQL> select count(nvl(comm, -1)) "number of null values"
  2  from emp
  3  where nvl(comm, -1) = -1;

number of null values
---------------------
                   10

我使用值“-1”作为空值的“别名”,因为我知道“-1”不是 comm 列中的现有值。

编辑:

遵循罗布的建议。可以从上面的示例中删除 where 子句并使用 NVL2功能如下图:

SQL> select count(nvl2(comm,null,-1)) "number of null values"
  2  from emp
  3  /

number of null values
---------------------
                   10

The Oracle documentation states that:

All aggregate functions except
COUNT(*) and GROUPING ignore nulls.
You can use the NVL function in the
argument to an aggregate function to
substitute a value for a null.

As an example, using the scott schema:

SQL> select empno, sal, comm
  2  from emp;

     EMPNO        SAL       COMM
---------- ---------- ----------
      7369        800
      7499       1600        300
      7521       1250        500
      7566       2975
      7654       1250       1400
      7698       2850
      7782       2450
      7788       3000
      7839       5000
      7844       1500          0
      7876       1100
      7900        950
      7902       3000
      7934       1300

14 rows selected.

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.

SQL> select count(nvl(comm, -1)) "number of null values"
  2  from emp
  3  where nvl(comm, -1) = -1;

number of null values
---------------------
                   10

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:

SQL> select count(nvl2(comm,null,-1)) "number of null values"
  2  from emp
  3  /

number of null values
---------------------
                   10
伏妖词 2024-09-08 20:34:14

如果您也想使用 null 来计算其他值,则使用 COALESCE 函数将缩短执行时间

Oracle NVL 和 Coalesce 之间的差异

SELECT COUNT(COALESCE( _COLUMN, 1)) AS CNT FROM _TABLE

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

SELECT COUNT(COALESCE( _COLUMN, 1)) AS CNT FROM _TABLE
夜巴黎 2024-09-08 20:34:14

我可能会尝试反转 null,查看结果

SELECT
 COUNT(DECODE(YourField, null, 1, null)) Nulls,
 count(*) Everything,
 COUNT(YourField) NotNulls
FROM YourTable

一切都应该等于 nulls + notnulls

I might try to inverse the null, see results

SELECT
 COUNT(DECODE(YourField, null, 1, null)) Nulls,
 count(*) Everything,
 COUNT(YourField) NotNulls
FROM YourTable

Everything should equal nulls + notnulls

二智少女猫性小仙女 2024-09-08 20:34:14
select count(nvl(values, 0)) from emp where values is null;
select count(nvl(values, 0)) from emp where values is null;
空气里的味道 2024-09-08 20:34:14

功能:

create or replace function xxhrs_fb_count_null
return number
as
l_count_null number;
begin
  select count(*) into l_count_null from emp where comm is null;
  return l_count_null;
end;

用途:

select xxhrs_fb_count_null from dual

Function:

create or replace function xxhrs_fb_count_null
return number
as
l_count_null number;
begin
  select count(*) into l_count_null from emp where comm is null;
  return l_count_null;
end;

Usage:

select xxhrs_fb_count_null from dual
陪你搞怪i 2024-09-08 20:34:14

我相信您的要求如下:
emp 有 100 行。对于 20 名员工,HIRE_DATE 列为 NULL。所以基本上,你想要得到 20 作为输出。
这是除了本论坛其他贡献者给出的答案之外的另一种方法。

-- COUNT (1) would return 100
-- COUNT (hire_date) would return 80
-- 100 - 80 = 20
SELECT   COUNT (1) -
                 COUNT (hire_date)
                 AS null_count
FROM      emp;

I believe your requirement is as below:
Table emp has 100 rows. Against 20 employees, HIRE_DATE column is NULL. So basically, you want to get 20 as output.
This is another method apart from the answers given by other contributors in this forum.

-- COUNT (1) would return 100
-- COUNT (hire_date) would return 80
-- 100 - 80 = 20
SELECT   COUNT (1) -
                 COUNT (hire_date)
                 AS null_count
FROM      emp;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文