Re: More wacky grouping

From: Manfred Koizar <mkoi-pg(at)aon(dot)at>
To: "James Taylor" <jtx(at)hatesville(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: More wacky grouping
Date: 2003-04-04 07:20:48
Message-ID: cabq8v0sntirhbn21jd9o2gme1gq2goi7k@4ax.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, 3 Apr 2003 16:29:08 -0800, "James Taylor" <jtx(at)hatesville(dot)com>
wrote:
>My table looks something like this:
>
>Id | id_customer | id_datatype | content
>----------------------------------------
>1 | 25 | 1 | John
>2 | 25 | 2 | Globochem
>3 | 25 | 3 | 845.92
>[...]
>Datatype 1 = Name, 2 = Company, 3 = account balance.
>
>I need to do an aggregate function on content where id_datatype = 3, and
>then group by content where id_datatype = 2

SELECT c.content AS "Company", sum(b.content::float) AS "Sum"
FROM james AS c LEFT JOIN james AS b
ON (c.id_customer = b.id_customer AND b.id_datatype = 3)
WHERE c.id_datatype = 2
GROUP BY c.content;

should work, if there is exactly one row with id_datatype = 2 for each
id_customer.

Servus
Manfred

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Christoph Haller 2003-04-04 09:21:15 Re: string variable with spaces in psql
Previous Message Joe Conway 2003-04-04 05:58:13 Re: More wacky grouping