0

I have a table that looks like this

Indiv_id     Trip_band    Fav1    Fav2    Fav3    Fav4    Fav5  
1234    0-90    386568    null    null    null    568889  
5678    91-180    null    889546    887456    856234   null

I would like to add another column that counts the amount of null values within a particular row and have my output look like this:

Indiv_id     Trip_band    Fav1    Fav2    Fav3    Fav4    Fav5    null_count  
1234    0-90    386568    null    null    null    568889    3  
5678    91-180    null    889546    887456    856234    null    2  

Is this possible to achieve?

Thanks in advance

3 Answers3

1

you can try like below by using case

 select t.*, 
        case when t.col1 is null then 1 
                                 else 0 end 
        + case when t.col2 is null then 1 else 0 end 
        + ........ 
        + case when coln is null then 1 else 0 end as null_count
   from table t

so you need to use your all column name like the upper descriptive way

Alex Poole
  • 183,384
  • 11
  • 179
  • 318
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
1

You can add together the result of multiple case expressions, one per column you want to check:

-- CTE for sample data
with your_table (Indiv_id, Trip_band, Fav1, Fav2, Fav3, Fav4, Fav5  ) as (
  select 1234, '0-90', 386568, null, null, null, 568889 from dual
  union all
  select 5678, '91-180', null, 889546, 887456, 856234, null from dual
)
-- actual query
select t.*,
    case when fav1 is null then 1 else 0 end
  + case when fav2 is null then 1 else 0 end
  + case when fav3 is null then 1 else 0 end
  + case when fav4 is null then 1 else 0 end
  + case when fav5 is null then 1 else 0 end
  as null_count
from your_table t;

  INDIV_ID TRIP_B       FAV1       FAV2       FAV3       FAV4       FAV5 NULL_COUNT
---------- ------ ---------- ---------- ---------- ---------- ---------- ----------
      1234 0-90       386568                                      568889          3
      5678 91-180                889546     887456     856234                     2

or you could use the Oracle-specific nvl2() function:

select t.*,
    nvl2(fav1, 0, 1)
  + nvl2(fav2, 0, 1)
  + nvl2(fav3, 0, 1)
  + nvl2(fav4, 0, 1)
  + nvl2(fav5, 0, 1)
  as null_count
from your_table t;

  INDIV_ID TRIP_B       FAV1       FAV2       FAV3       FAV4       FAV5 NULL_COUNT
---------- ------ ---------- ---------- ---------- ---------- ---------- ----------
      1234 0-90       386568                                      568889          3
      5678 91-180                889546     887456     856234                     2

but the case expressions are clearer to me.

There isn't a built-in function that can be applied to all columns in a row at once though, you need to check them all individually. (I guess you could unpivot, count, and pivot back - but that's a lot of extra work and you still need to list the columns anyway...)

Alex Poole
  • 183,384
  • 11
  • 179
  • 318
0

In Oracle 12C+, you can use a lateral join:

select x.*, c.cnt
from t cross join lateral
     (select count(*) as cnt
      from (select x.fav1 as fav from dual union all
            select x.fav2 from dual union all
            select x.fav3 from dual union all
            select x.fav4 from dual union all
            select x.fav5 from dual union all
           ) x
      where fav is not null
     ) c;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786