pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: 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:16:01 -0800
Message-ID: <200304040816.01369.josh@agliodbs.com> (raw)
In-Reply-To: <20030404080209.M3259@klaster.net>
References: <1049403724.13799.5473.camel@camel>
	<20030404080209.M3259@klaster.net>

Tomasz,

> 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;

That version is only more efficient for small data sets.    I've generally 
found that case statements are slower than subselects for large data sets. 
YMMV.

BTW, while it won't be faster, Joe Conway's crosstab function in /tablefunc 
does this kind of transformation.

-- 
Josh Berkus
Aglio Database Solutions
San Francisco




view thread (8+ messages)  latest in thread

Message-ID: <200304040816.01369.josh@agliodbs.com>
Permalink:  ../200304040816.01369.josh@agliodbs.com/
Also on:    postgresql.org/message-id/200304040816.01369.josh@agliodbs.com

 · 

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

* 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