Solved

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

Posted on 2004-11-03
6,499 Views
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';)
0
Question by:gripe

LVL 3

Accepted Solution

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

LVL 13

Author Comment

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

LVL 3

Expert Comment

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

## Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

## Featured Post

### Suggested Solutions

Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: http://www.postgresql.org/ (http://www.postgresql.org/) This requires some add-o…
Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

#### 759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!