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

  • Home
  • SEARCH
  • 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 8680329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:13:17+00:00 2026-06-12T21:13:17+00:00

I have been counting NULL and non-NULL columns with a subselect and some aggregate

  • 0

I have been counting NULL and non-NULL columns with a subselect and some aggregate functions

CREATE TEMPORARY TABLE citizens(name text, country text,profession text,postalcode text);

INSERT INTO citizens VALUES
 ('Fred', 'USA', 'Professor', NULL),
 ('Amy', 'USA', 'Professor', NULL),
 ('Ted', 'USA', 'Professor', 90210),
 ('Barb', 'USA', 'Lawyer', 10248),
 ('Wally', 'USA', 'Lawyer', NULL),
 ('Fred', 'Canada', 'Professor', 'S0H'),
 ('Charles', 'Canada', 'Professor', 'S4L'),
 ('Nancy', 'Canada', 'Lawyer', NULL),
 ('Linda', 'Canada', 'Professor', NULL),
 ('Steph', 'France', 'Lawyer', 75008 ),
 ('Arnold', 'France', 'Lawyer', 75008 ),
 ('Penny', 'France', 'Lawyer', 75008 ),
 ('Harry', 'France', 'Lawyer', NULL);

SELECT country,
    profession,
    MAX(have_postalcode::int*num) AS num_have,
    MAX((1-have_postalcode::int)*num) AS num_not_have
FROM
    (
    SELECT country, profession,
        COUNT(*) AS num,
        (postalcode IS NOT NULL) AS have_postalcode
    FROM citizens
    GROUP BY country, profession, have_postalcode
    ) AS d
GROUP BY country, profession

with the result

USA     Professor   1   2
Canada  Lawyer      0   1
USA     Lawyer      1   1
France  Lawyer      3   1
Canada  Professor   2   1

but it seems like there ought to be a slicker way (for example it pains me that MAX is used just to grab the one nontrivial value). Does anybody have a cool idea?

  • 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-12T21:13:18+00:00Added an answer on June 12, 2026 at 9:13 pm
    SELECT country, profession,
            COUNT(postalcode) AS num_have
          , (COUNT(*) - COUNT(postalcode)) AS num_not_have
    FROM citizens
    GROUP BY country, profession;
    

    http://sqlfiddle.com/#!1/17a9d/15

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
I have been using TortoiseSVN for some time and I really like it. I
I have a table with this column (among others): id int identity(1,1) not null
I have been looking round and I'm able to find ways of counting lines
I have been struggling to understand some pieces of this code. It asks to
I have a tab delimited table that has multiple columns (1500) and a few
I have been working on counting a list of words in my corpus, and
Hello I seem to be having some problem involving counting in Django. I have
Automatic Reference Counting releases Objective-C objects that have been allocated. What about primitive variables
I have been trying to implement counting sort. It requests the user for the

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.