Solved

Best method to identify nulls in columns in all columns

Posted on 2014-11-25
7
134 Views
Last Modified: 2014-11-25
I have a table with about 30+ columns.  Of all those columns I want to check every column to see if all the rows have nulls in them.
I want to display all the column names and if they were all nulls or not.

I started with a select statement with Case's but was not getting what I was hoping for so I figured there was a better way.

Any advice would be great!

Thanks in advance.

--Sample table
select AKAFirst,
AKAMiddle,
AKASurname,
AltAssess,
BirthCountry,
BirthPlace_City,
Birthplace_StateProvince,
CELDT_TestPurpose,
CMA_CSTTest,
CMA_ELA
from S_CA_STU_X;

--Display something like this:  (comments would not show ect)
AKAFirst    0               --this would be that all rows were null
AKAMiddle    8          --this would be 8 rows have data
AKASurname   15     --this wold be 15 rows have data
AltAssess   0              --this would be that all rows were null
...
0
Comment
Question by:wayneburr
  • 3
  • 2
  • 2
7 Comments
 
LVL 31

Expert Comment

by:awking00
ID: 40465204
You might use a procedure like the following:
set serveroutput on
declare
v_sql varchar2(255);
v_cnt number;
begin
for c in
(select table_name, column_name
 from user_tab_columns
 where table_name = 'YOURTABLE'
 order by 1, 2)
loop
v_sql := 'select count('||c.column_name||') from '||c.table_name;
execute immediate v_sql into v_cnt;
dbms_output.put_line(c.column_name||'    '||v_cnt);
end loop;
end;
/
0
 

Author Comment

by:wayneburr
ID: 40465249
Thanks awking!

I dont think I want to go that route with a procedure if I can help it.  I am doing some testing on different machines and multiple databases. I have to port my query to each sid quickly, test, get results and get out of there ect.

This is what I came up with but worry that it might be much with 30+ columns:
select case when (select count("BIRTHCOUNTRY") from "PS"."S_CA_STU_X" group by "BIRTHCOUNTRY") > 1 then 1 else 0 end as Birthcountry
  from "PS"."S_CA_STU_X"
  group by Birthcountry;
0
 

Author Comment

by:wayneburr
ID: 40465293
So my other attempt did not work like I was hoping to either when I had values in the columns that were not null.   There was an error as I'm sure most of you would see why, so I changed it to this:  (But now I get multiple rows)
select case when (select count(akafirst) from "PS"."S_CA_STU_X" where akafirst is not null) > 1 then 1 else 0 end as AKAFirst,
  case when (select count(AKAMIDDLE) from "PS"."S_CA_STU_X" where akafirst is not null) > 1 then 1 else 0 end as AKAMIDDLE
  from "PS"."S_CA_STU_X"
  group by AKAFirst, AKAMIDDLE;

Data looks like this:
akafirst         akamiddle
1                     1
1                      1
1                      1
...
--for 300+ rows

I only want 1 row per column list to count the number of non nulls
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 34

Accepted Solution

by:
johnsone earned 500 total points
ID: 40465310
What about something like this:

SELECT SUM(CASE 
             WHEN akafirst IS NOT NULL THEN 1 
             ELSE 0 
           END) AKAFirst, 
       SUM(CASE 
             WHEN akamiddle IS NOT NULL THEN 1 
             ELSE 0 
           END) AKAMiddle, 
       SUM(CASE 
             WHEN akasurname IS NOT NULL THEN 1 
             ELSE 0 
           END) AKASurname, 
       SUM(CASE 
             WHEN altassess IS NOT NULL THEN 1 
             ELSE 0 
           END) AltAssess, 
       SUM(CASE 
             WHEN birthcountry IS NOT NULL THEN 1 
             ELSE 0 
           END) BirthCountry, 
       SUM(CASE 
             WHEN birthplace_city IS NOT NULL THEN 1 
             ELSE 0 
           END) BirthPlace_City, 
       SUM(CASE 
             WHEN birthplace_stateprovince IS NOT NULL THEN 1 
             ELSE 0 
           END) Birthplace_StateProvince, 
       SUM(CASE 
             WHEN celdt_testpurpose IS NOT NULL THEN 1 
             ELSE 0 
           END) CELDT_TestPurpose, 
       SUM(CASE 
             WHEN cma_csttest IS NOT NULL THEN 1 
             ELSE 0 
           END) CMA_CSTTest, 
       SUM(CASE 
             WHEN cma_ela IS NOT NULL THEN 1 
             ELSE 0 
           END) CMA_ELA 
FROM   s_ca_stu_x; 

Open in new window

0
 

Author Closing Comment

by:wayneburr
ID: 40465338
Thank you everybody!

Johnsone, Thank you  as this is exactly what I was looking for!

Laid it out and this is what it looks like:
select
sum(case when AKAFirst is Not null then 1 else 0 end) as AKAFirst,
 sum(case when AKAMIDDLE is Not null then 1 else 0 end) as AKAMIDDLE,
 sum(case when AKASURNAME is Not null then 1 else 0 end) as AKASURNAME,
 sum(case when BIRTHCOUNTRY is Not null then 1 else 0 end) as BIRTHCOUNTRY
 from "PS"."S_CA_STU_X" ;
0
 
LVL 31

Expert Comment

by:awking00
ID: 40465368
The problem with trying to use a slelect is that your results will be horizontal like -
col1     col2      col3    .... col30+      
value   value   value   .... value
You might try using the procedure to load a table rather than printing out to the console.
Create table newtable(colname varchar2(30), notnulcount number);
Just change this
dbms_output.put_line(c.column_name||'    '||v_cnt);
to this
insert into newtable values(c.column_name,v_cnt);
Then you can query the table -
select * from newtable.
0
 
LVL 34

Expert Comment

by:johnsone
ID: 40465476
While I think the procedure way would work well and would get the results, I see two things.

A good thing, it is dynamic and will get the column names and figure everything out.

A bad thing, it passes the table once for each column.

If you wanted to go that route, I would have the procedure build a SQL statement for the entire table and then execute it.  Getting all the counts in one pass of the table.

The single SQL statement requires quite a bit of editing to make if you need multiple tables, but is a quick way to pass the table only once to get the results.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now