Possible to do this in one query?

From: "John Oakes" <john(at)networkproductions(dot)net>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Possible to do this in one query?
Date: 2002-03-26 18:22:41
Message-ID: 001301c1d4f3$37bd5160$f8832341@john
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I have a table with a column that contains fail codes. These codes are 1-9.
I need to get a count of each of the codes. Right now I have separate
queries:

select count(*) from tablename
where failcode = '1';

If I use GROUP BY it obviously doesn't get the count for codes that don't
exist. I need to have 0 returned though so I have a listing of all 9 fail
codes and their total, even if it is zero. I tried UNION like this:

select count(*) from tablename
where failcode = '1';
UNION
select count(*) from tablename
where failcode = '2';

etc, but it too excludes the codes that have a count of zero. Right now I
have resorted to using 9 separate queries. Is there a way to do this with
one? Thank you!

John Oakes

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Peter Atkins 2002-03-26 19:55:50 Changeing Sequence
Previous Message Josh Berkus 2002-03-26 16:45:53 Re: Cleaning up template 1