Re: Count of non-null values per table column

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: David Rowley <david(dot)rowley(at)2ndquadrant(dot)com>
Cc: David Nelson <dlnelson77808(at)outlook(dot)com>, "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 15:00:59
Message-ID: 4333.1439564459@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

David Rowley <david(dot)rowley(at)2ndquadrant(dot)com> writes:
> On 15 August 2015 at 02:32, David Nelson <dlnelson77808(at)outlook(dot)com> wrote:
>> Hello list,<br><br>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.

> I assume the tables are quite large if you don't want to just issue a:
> SELECT COUNT(a) FILTER(WHERE a IS NULL),COUNT(b) FILTER(WHERE b IS NULL)
> ... (assuming you're on a version new enough to support agg FILTER)

AFAIK this should work in any version, or indeed any SQL-compliant DBMS:

select count(col1), count(col2), ... from table;

COUNT with an argument counts the non-null values of that argument.

> On the other hand if you were happy with just an approximation then you
> could look at pg_stats;

Yeah; you might want to ANALYZE the table first to be sure the stats are
up to date.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jeff Janes 2015-08-14 15:53:34 Re: Extension to rewrite queries before execution
Previous Message John McKown 2015-08-14 14:59:44 Re: Count of non-null values per table column