Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9124395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:33:44+00:00 2026-06-17T06:33:44+00:00

I am using a proprietary mpp database that has been forked off psql 8.3

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:33:46+00:00Added an answer on June 17, 2026 at 6:33 am

    First, since COUNT() only counts non-null values, your query can be simplified:

    SELECT count(DISTINCT names) AS unique_names
          ,count(names) AS names_not_null
    FROM   table;
    

    But that’s the number of non-null values and contradicts your description:

    count of the number of null values in the column

    For that you would use:

    count(*) - count(names) AS names_null
    

    Since count(*) count all rows and count(names) only rows with non-null names.
    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 use EXECUTE 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:

    • How to perform the same aggregation on every column, without listing the columns?
    • postgresql – count (no null values) of each column in a table
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the proprietary filter css in IE (7/8). Trying to apply an additional filter
I'm using a proprietary library that has an openConnection function that I use as
I'm using a proprietary platform that reported memory usage in realtime on screen. I
I'm connecting to an proprietary MSSQL database using python, sqlalchemy and pymssql. The database
I have a friend who had been using a proprietary photo album product and
I was using a proprietary rule engine, and I am now trying to use
We're using a back-end proprietary database to feed data to over 100 dynamic pages
We're using a proprietary tracking system that requires the use of regular expressions to
I'm trying to work on project using a proprietary mvc and we have a
We are about to migrate an intranet web application from using a proprietary forms-based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.