Re: Count of non-null values per table column

From: David Nelson <dlnelson77808(at)outlook(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Count of non-null values per table column
Date: 2015-08-14 14:38:21
Message-ID: COL126-W3799A33B6369CDCDE76578F17C0@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Well it is certainly nice to see that my choice to send my question using plain text was honored by this email service. Apologies for that mess. The output I am looking for is a series of rows with two columns, one the name of the table column, and the other the count of non-null values in a table's column of that same name, for all column names in the table.<br><br>Thanks<br><br>----------------------------------------<br>&gt; From: dlnelson77808(at)outlook(dot)com<br>&gt; To: pgsql-general(at)postgresql(dot)org<br>&gt; Subject: [GENERAL] Count of non-null values per table column<br>&gt; Date: Fri, 14 Aug 2015 14:32:36 +0000<br>&gt;<br>&gt; Hello list,&lt;br&gt;&lt;br&gt;Apologies if this has been asked before. My search only turned up ways to list the total non-null values for all columns as a single number. I want the count for each column by column.&lt;br&gt;&lt;br&gt;I have inherited a database consisting of two related huge monolithic tables that lack referential integrity between them, or even basic data constraints. One of the problems these tables have is every single non-PK column is NULLable. I am trying to understand the information that is actually stored and used so that I can implement a (hopefully) better design. Towards that end I would like to know the count of non-null values in each column per column. In other words I would like to get the following output from a table (the numbers are totally made up):&lt;br&gt;&lt;br&gt;column_name | num_values&lt;br&gt;------------+-----------&lt;br&gt;col1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5787&lt;br&gt;------------+-----------&lt;br&gt;col2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 17&lt;br&gt;------------+-----------&lt;br&gt;col3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 567&lt;br&gt;------------+-----------&lt;br&gt;col4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5787&lt;br&gt;------------+-----------&lt;br&gt;col5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 143&lt;br&gt;------------+-----------&lt;br&gt;col6&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;br&gt;------------+-----------&lt;br&gt;...&lt;br&gt;&lt;br&gt;Is this possible through one or more of the system views, or will I need to write a function to do this? Obviously I can just issue multiple SELECT COUNT(column)... statements, but I'd rather not.&lt;br&gt;&lt;br&gt;Thanks,&lt;br&gt;David<br>&gt;<br>&gt; --<br>&gt; Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)<br>&gt; To make changes to your subscription:<br>&gt; http://www.postgresql.org/mailpref/pgsql-general<br>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Rowley 2015-08-14 14:51:57 Re: Count of non-null values per table column
Previous Message David Nelson 2015-08-14 14:32:36 Count of non-null values per table column