I'm trying to query a Netezza table to get count of rows where a column value is NULL -- for all columns. Concretely, assume we have the following table (titled, say, merchants
)
business_name phone email
-------------------------------------------------------
NULL 505-844-1234 john@example.com
Alibaba NULL mary@domain.com
NULL NULL harry@company.com
I want to generate an output table like:
column_name NULL_count
-------------------------------
business_name 2
phone 2
email 0
I can generate it for individual columns using:
select count(*)
from merchants
where <column_name> is null;
However, my table has over 100 columns and I don't want to hand code the queries. I'm aware that I can write a Java/Python code to query the table programmatically or even write a script to generate ~100 queries. However, the task does not seem overly complicated and I feel it should be achievable in pure SQL. The list of columns for every Netezza table is available via:
SELECT column_name
FROM information_schema.columns
WHERE LOWER(table_name) = 'merchants'`
I want to run the above count(*)
query for every column in the above list. I'm having difficulty figuring the proper join and/or using stored procedure. So far, I've tried modifying a Netezza stored procedure as explained here, but I keep getting syntax errors on the original code listed there.
TL;DR How can I generate null count for all columns in Netezza ?