Single query to return 'count of all rows', 'count of distinct value in row' and 'count of rows having value'

If I have a table 'TABLE' like the following:
 
FOO BAR BAZ
-----  ----- ----
A      Y      Z
B      Y      Z
B      Y      Q
C      Y      Q
C      Y      Q
 
I would like to write a single select statement that returns three values in one row:

count(distinct foo) (count of all distinct values in FOO) (Would return 3 in this example)
count() (count of all rows) (Would return 5 in this example)
count() (count of all BAZ containing 'Q') (group by baz having baz = 'Q' / Would return 3 in this example)
 
Make sense? Any single statement do that or must this be split into two seperate queries?
(IE: select count(distinct foo), count(1) from TABLE;  select count(1) from TABLE group by BAZ having BAZ = 'Q';)
LVL 13
gripeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ctcampbellCommented:
Use SUM(CASE baz WHEN 'Q' THEN 1 ELSE 0 END).

--- begin postgres code ---
/*
create table table1 (
      foo char(1) not null,
      bar char(1) not null,
      baz char(1) not null
);

insert into table1 (foo, bar, baz) values ('A', 'Y', 'Z');
insert into table1 (foo, bar, baz) values ('B', 'Y', 'Z');
insert into table1 (foo, bar, baz) values ('B', 'Y', 'Q');
insert into table1 (foo, bar, baz) values ('C', 'Y', 'Q');
insert into table1 (foo, bar, baz) values ('C', 'Y', 'Q');
*/

select count(distinct foo), count(*), sum(case baz when 'Q' then 1 else 0 end)
  from table1;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gripeAuthor Commented:
Thanks a lot.. didn't know about tthose case blocks, but that will definitely come in handy.

Is this a postgres thing? Do you know where it's documented?

0
ctcampbellCommented:
CASE is part of the ANSI SQL standard and fully supported in Postgres.  You should be able to use it anywhere you would use an expression, such as in a function call (demonstrated here), on the right side of an assignment (SET field = CASE ...), or in a WHERE clause (WHERE x = CASE ...).  There are too many situations to list.  Wherever the docs say "expression", you should be able to use CASE.

A similar use to your question is for cross-tab queries:
SELECT
SUM(CASE foo WHEN 'A' THEN 1 ELSE 0 END) AS total_a,
SUM(CASE foo WHEN 'B' THEN 1 ELSE 0 END) AS total_b,
SUM(CASE foo WHEN 'C' THEN 1 ELSE 0 END) AS total_c
COUNT(*) AS total_all
FROM table1

Here is a link to the docs:
http://www.postgresql.org/docs/7.4/static/functions-conditional.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PostgreSQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.