oracle:计算一行中空字段的数量

发布于 2024-10-21 09:40:42 字数 353 浏览 2 评论 0原文

我有一个有 5 个“可选”字段的表。我想知道有多少行全部 5 个为空,有多少行有 1 个字段非空,等等。

我尝试了一些方法,例如:

select 
 count(*),
 ( (if field1 is null then 1 else 0) +
   (if field2 is null then 1 else 0) +
 etc.

但这当然行不通。

理想情况下,我正在寻找类似的输出

Nulls   Cnt
0        200
1        345
...
5        40

是否有一个优雅的解决方案?

I have a table that has 5 "optional" fields. I'd like to find out how many rows have all 5 null, how many have 1 field non-null, etc.

I've tried a couple of things, like:

select 
 count(*),
 ( (if field1 is null then 1 else 0) +
   (if field2 is null then 1 else 0) +
 etc.

but of course that doesn't work.

Ideally, I'm looking for output that's something like

Nulls   Cnt
0        200
1        345
...
5        40

Is there an elegant solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

女皇必胜 2024-10-28 09:40:42

关键字不是 if,而是 case,并且必须使用 end 结束 case 语句。

这是一个适合您的查询:

WITH subQuery AS
(
  SELECT My_Table.*, (CASE WHEN My_Table.field1 IS NULL THEN 1 ELSE 0 END +
                      CASE WHEN My_Table.field2 IS NULL THEN 1 ELSE 0 END +
                      CASE WHEN My_Table.field3 IS NULL THEN 1 ELSE 0 END +
                      CASE WHEN My_Table.field4 IS NULL THEN 1 ELSE 0 END +
                      CASE WHEN My_Table.field5 IS NULL THEN 1 ELSE 0 END ) NumberOfNullFields
    FROM My_Table
)
SELECT NumberOfNullFields, COUNT(*)
  FROM subQuery
 GROUP BY NumberOfNullFields;

The keyword is not if, it is case, and you must use end to end the case statement.

Here is a query that can suit you:

WITH subQuery AS
(
  SELECT My_Table.*, (CASE WHEN My_Table.field1 IS NULL THEN 1 ELSE 0 END +
                      CASE WHEN My_Table.field2 IS NULL THEN 1 ELSE 0 END +
                      CASE WHEN My_Table.field3 IS NULL THEN 1 ELSE 0 END +
                      CASE WHEN My_Table.field4 IS NULL THEN 1 ELSE 0 END +
                      CASE WHEN My_Table.field5 IS NULL THEN 1 ELSE 0 END ) NumberOfNullFields
    FROM My_Table
)
SELECT NumberOfNullFields, COUNT(*)
  FROM subQuery
 GROUP BY NumberOfNullFields;
人海汹涌 2024-10-28 09:40:42

虽然计数时的情况没有任何问题,但我只是想看看是否还有其他方法。

WITH SAMPLEDATA AS(--just generate some data with nulls for 5 cols
    select  level ,
            (case when mod(level,2) = 0 then 1 else null end) colA,
            (case when mod(level,3) = 0 then 1 else null end) colB,
            (case when mod(level,5) = 0 then 1 else null end) colC,
            (case when mod(level,7) = 0 then 1 else null end) colD,
            (case when mod(level,11) = 0 then 1 else null end) colE

      from dual
      connect by level < 1000
    ), --utilize the count(Aggregate)'s avoidance of nulls to our summation advantage
    nullCols as(
    SELECT COUNT(COLA) aNotNull
           ,cOUNT(*)-COUNT(COLA) aNull
           ,count(colB) bNotNull
           ,cOUNT(*)-count(colB) bNull
           ,count(colc) cNotNull
           ,cOUNT(*)-count(colc) cNull
           ,count(cold) dNotNull
           ,cOUNT(*)-count(cold) dNull
           ,count(cole) eNotNull
           ,cOUNT(*)-count(cole) eNull
           , cOUNT(*) TotalCountOfRows
     from SAMPLEDATA  )
    SELECT (select count(*) from sampledata where cola is null and colb is null and colc is null and cold is null and cole is null) allIsNull     
           ,nullCols.*
    FROM nullCols;

ALLISNULL              ANOTNULL               ANULL                  BNOTNULL               BNULL                  CNOTNULL               CNULL                  DNOTNULL               DNULL                  ENOTNULL               ENULL                  TOTALCOUNTOFROWS       
---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- 
207                    499                    500                    333                    666                    199                    800                    142                    857                    90                     909                    999                    

这利用了

如果计数中的表达式(表达式)
计算结果为 null,则不计算在内:
正如此处

所述

,这种方法,正如上面显而易见的那样,不能“雄辩地”很好地总结所有空列。只是想看看如果没有 CASE 逻辑这是否可行。

While there is nothing wrong with the case WHEN counting, I just wanted to see if there was another way.

WITH SAMPLEDATA AS(--just generate some data with nulls for 5 cols
    select  level ,
            (case when mod(level,2) = 0 then 1 else null end) colA,
            (case when mod(level,3) = 0 then 1 else null end) colB,
            (case when mod(level,5) = 0 then 1 else null end) colC,
            (case when mod(level,7) = 0 then 1 else null end) colD,
            (case when mod(level,11) = 0 then 1 else null end) colE

      from dual
      connect by level < 1000
    ), --utilize the count(Aggregate)'s avoidance of nulls to our summation advantage
    nullCols as(
    SELECT COUNT(COLA) aNotNull
           ,cOUNT(*)-COUNT(COLA) aNull
           ,count(colB) bNotNull
           ,cOUNT(*)-count(colB) bNull
           ,count(colc) cNotNull
           ,cOUNT(*)-count(colc) cNull
           ,count(cold) dNotNull
           ,cOUNT(*)-count(cold) dNull
           ,count(cole) eNotNull
           ,cOUNT(*)-count(cole) eNull
           , cOUNT(*) TotalCountOfRows
     from SAMPLEDATA  )
    SELECT (select count(*) from sampledata where cola is null and colb is null and colc is null and cold is null and cole is null) allIsNull     
           ,nullCols.*
    FROM nullCols;

ALLISNULL              ANOTNULL               ANULL                  BNOTNULL               BNULL                  CNOTNULL               CNULL                  DNOTNULL               DNULL                  ENOTNULL               ENULL                  TOTALCOUNTOFROWS       
---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- 
207                    499                    500                    333                    666                    199                    800                    142                    857                    90                     909                    999                    

this utilizes the

If expression in count(expression)
evaluates to null, it is not counted:
as noted from here

This method, as is obvious above, does not 'eloquently' sum all null columns well. Just wanted to see if this was possible without the CASE logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文