Re: aggregating into 2 dimensions?

From: Harry <h3(at)x-maru(dot)org>
To: "Henshall, Stuart - Design & Print" <SHenshall(at)westcountry-design-print(dot)co(dot)uk>, pgsql-novice(at)postgresql(dot)org
Subject: Re: aggregating into 2 dimensions?
Date: 2002-12-11 08:43:17
Message-ID: 20021211004317.4e72a3d2.h3@x-maru.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Tue, 10 Dec 2002 13:47:22 -0000
"Henshall, Stuart - Design & Print" <SHenshall(at)westcountry-design-print(dot)co(dot)uk>
wrote:

> > I'd like to perform a query that aggregates/counts based on 2 of my
> > columns.

[snip]

> SELECT dpa.dt,dpa.tot AS depa,dpb.tot AS depb,dpc.tot AS depc FROM
> (SELECT dt,count(item) AS tot FROM tbl GROUP BY dt WHERE dep=a) AS dpa,
> (SELECT dt,count(item) AS tot FROM tbl GROUP BY dt WHERE dep=b) AS dpb,
> (SELECT dt,count(item) AS tot FROM tbl GROUP BY dt WHERE dep=c) AS dpc WHERE
> dpa.dt=dpb.dt AND dpa.dt=dpc.dt;

Excellent! Just what I was looking for. The only change required was to put the
"group by" after the "where" and it works like a dream - speedy too (unlike the
other kludgy queries I'd devised)

Thanks much - this will be a useful addition to my bag of tricks.

-Harry

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Pam Wampler 2002-12-11 14:43:32 pgmonitor and pgadmin
Previous Message Conxita Marín 2002-12-11 08:21:03 plpgsql: How to catch error when I drop an inexistent table?