[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
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
3
Medium Priority
?
6,503 Views
Last Modified: 2008-01-09
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
Comment
Question by:gripe
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
ctcampbell earned 2000 total points
ID: 12489176
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

by:gripe
ID: 12489213
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

by:ctcampbell
ID: 12489612
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
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.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

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

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

Join & Ask a Question