pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomasz Myrta <jasiek@klaster.net>
To: Robert Treat <xzilla@users.sourceforge.net>
To: pgsql-sql@postgresql.org
Subject: Re: can i make this sql query more efficiant?
Date: Fri, 4 Apr 2003 08:02:09 +0900
Message-ID: <20030404080209.M3259@klaster.net> (raw)
In-Reply-To: <1049403724.13799.5473.camel@camel>
References: <1049403724.13799.5473.camel@camel>

<cut>
> select 
> 	event, 
> 	(select count(*) from baz a 
> 		where level = 1 and a.event=baz.event) as ones, 
> 	(select count(*) from baz a 
> 		where level = 2 and a.event=baz.event) as twos, 
> 	(select count(*) from baz a 
> 		where level = 3 and a.event=baz.event) as threes
> from
> 	 baz
> group by 
> 	event;
> 
> which gives me:
> 
>  event | ones | twos | threes 
> -------+------+------+--------
>  x     |    1 |    1 |      1
>  y     |    0 |    1 |      2
> (2 rows)
<cut>
What about this:
select
  event,
  sum(case when level=1 then 1 else 0 end) as ones,
  sum(case when level=2 then 1 else 0 end) as twos,
  sum(case when level=3 then 1 else 0 end) as threes
from baz
group by event;

Regards,
Tomasz Myrta




view thread (8+ messages)  latest in thread

Message-ID: <20030404080209.M3259@klaster.net>
Permalink:  ../20030404080209.M3259@klaster.net/
Also on:    postgresql.org/message-id/20030404080209.M3259@klaster.net

 · 

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: jasiek@klaster.net, xzilla@users.sourceforge.net
  Subject: Re: can i make this sql query more efficiant?
  In-Reply-To: <20030404080209.M3259@klaster.net>

* 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