Re: Count of non-null values per table column - Mailing list pgsql-general

From Tom Lane
Subject Re: Count of non-null values per table column
Date
Msg-id 4333.1439564459@sss.pgh.pa.us
Whole thread Raw
In response to Re: Count of non-null values per table column  (David Rowley <david.rowley@2ndquadrant.com>)
Responses Re: Count of non-null values per table column
List pgsql-general
David Rowley <david.rowley@2ndquadrant.com> writes:
> On 15 August 2015 at 02:32, David Nelson <dlnelson77808@outlook.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


pgsql-general by date:

Previous
From: John McKown
Date:
Subject: Re: Count of non-null values per table column
Next
From: Sachin Srivastava
Date:
Subject: Re: PostgreSQL - The Best Overall Database