pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Christoph Haller <ch@rodos.fzk.de>
To: david.bouriaud@ac-rouen.fr
Cc: pgsql-sql@postgresql.org
Subject: Re: SQL problem with aggregate functions.
Date: Thu, 11 Jul 2002 10:37:40 METDST
Message-ID: <200207110837.KAA07881@rodos> (raw)
In-Reply-To: <200207091036.17913.david.bouriaud@ac-rouen.fr>

> 
> I've got a table in which there is a field that can have one amongst 3 
> possible values : D, R, X. Is it possible to get in one query the count of 
> this different values.Please, note that I don't want to have a querry like 
> this :
> "select count (*) from tab group by f1;", cause i want to get all the possible 
> count values in one row (these data are already grouped on another field).
> To give a more accurate example, here is what I want to retrieve :
> 
> Field group | count of D | count of R | count of X.
> 
> Any clues ?
> -- 
What about something like 


SELECT SUM(f1_d) AS count_d,
       SUM(f1_r) AS count_r,
       SUM(f1_x) AS count_x
FROM (
 SELECT CASE WHEN f1 = 'D' THEN 1 ELSE 0 END AS f1_d,
        CASE WHEN f1 = 'R' THEN 1 ELSE 0 END AS f1_r,
        CASE WHEN f1 = 'X' THEN 1 ELSE 0 END AS f1_x
 FROM tab ) AS foo ;

Regards, Christoph 



view thread (5+ messages)  latest in thread

Message-ID: <200207110837.KAA07881@rodos>
Permalink:  ../200207110837.KAA07881@rodos/
Also on:    postgresql.org/message-id/200207110837.KAA07881@rodos

 · 

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: ch@rodos.fzk.de, david.bouriaud@ac-rouen.fr
  Subject: Re: SQL problem with aggregate functions.
  In-Reply-To: <200207110837.KAA07881@rodos>

* 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