I am using a proprietary mpp database that has been forked off psql 8.3
. I am trying to apply a simple count to a wide table (around 450 columns) and so I was wondering if the best way to do this in terms of a simple sql function. I am just counting the number of distinct values in a given column as well as the count of the number of null values in the column. The query i want to generalize for every column is for example
If i want to run the query against the column names i write
select
count(distinct names) d_names,
sum(case when names is not null then 1 else 0 end) n_s_ip
from table;
How do i generalize the query above to iterate through every column in the table if the number of columns is 450 without writing out each column name by hand?
First, since
COUNT()
only counts non-null values, your query can be simplified:But that's the number of non-null values and contradicts your description:
For that you would use:
Since
count(*)
count all rows andcount(names)
only rows with non-nullnames
.Removed inferior alternative after hint by @Andriy.
To automate that for all columns build an SQL statement off of the catalog table
pg_attribute
dynamically. You can useEXECUTE
in a PL/pgSQL function to execute it immediately. Find full code examples with links to the manual and explanation under these closely related questions: