title: Generating NULL counts by looping over columns in Netezza
body: <p>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, <code>merchants</code>) </p>
  
  <pre><code>business_name    phone                 email
  -------------------------------------------------------
  NULL             505-844-1234         john@example.com
  Alibaba          NULL                 mary@domain.com
  NULL             NULL                 harry@company.com
  </code></pre>
  
  <p>I want to generate an output table like:</p>
  
  <pre><code>column_name          NULL_count
  -------------------------------
  business_name          2
  phone                  2
  email                  0
  </code></pre>
  
  <p>I can generate it for individual columns using:</p>
  
  <pre><code>select count(*)
  from merchants
  where &lt;column_name&gt; is null;
  </code></pre>
  
  <p>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: </p>
  
  <pre><code>SELECT column_name 
  FROM information_schema.columns 
  WHERE LOWER(table_name) = 'merchants'`
  </code></pre>
  
  <p>I want to run the above <code>count(*)</code> 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 <a href="https://stackoverflow.com/questions/21870410/for-loop-netezza-issue">here</a>, but I keep getting syntax errors on the original code listed there.</p>
  
  <p>TL;DR How can I generate null count for all columns in Netezza ?</p>