pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ian Barwick <barwick@gmx.net>
To: John Oakes <john@networkproductions.net>
To: pgsql-sql@postgresql.org
Subject: Re: Possible to do this in one query?
Date: Wed, 27 Mar 2002 03:10:50 +0100
Message-ID: <200203270309.EAA29988@post.webmailer.de> (raw)
In-Reply-To: <001301c1d4f3$37bd5160$f8832341@john>
References: <001301c1d4f3$37bd5160$f8832341@john>

On Tuesday 26 March 2002 19:22, John Oakes wrote:
> 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!

If you have a seperate table containing all possible fail codes you can do 
something like this:

select fc.failcode, count(tn.failcode) 
from failcode_table fc
left join tablename tn 
on tn.failcode=fc.failcode
group by fc.failcode


Ian Barwick



view thread (3+ messages)  latest in thread

Message-ID: <200203270309.EAA29988@post.webmailer.de>
Permalink:  ../200203270309.EAA29988@post.webmailer.de/
Also on:    postgresql.org/message-id/200203270309.EAA29988@post.webmailer.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: barwick@gmx.net, john@networkproductions.net
  Subject: Re: Possible to do this in one query?
  In-Reply-To: <200203270309.EAA29988@post.webmailer.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox