From: Bruno Wolff III <bruno@wolff.to>
To: Vitaly Belman <vitalib@012.net.il>
Cc: pgsql-performance@postgresql.org
Subject: Re: Additional select fields in a GROUP BY
Date: Sun, 13 Jun 2004 08:52:12 -0500
Message-ID: <20040613135212.GA3260@wolff.to> (raw)
In-Reply-To: <2393942843.20040613062117@012.net.il>
References: <2393942843.20040613062117@012.net.il>
On Sun, Jun 13, 2004 at 06:21:17 +0300,
Vitaly Belman <vitalib@012.net.il> wrote:
>
> Consider the following query:
>
> select t1field1, avg(t2fieild2)
> from t1, t2
> where t1.field1 = t2.field2
> group by t1field1
>
> That works fine. But I'd really like to see more fields of t1 in this
> query, however I can't add them into the select because they're not
> part of the GROUP BY, thus I have to add them to there too:
If t1.field1 is a candiate key for t1, then the normal thing to do is
to group t2 by t2.field1 (assuming you really meant to join on t2.field1,
not t2.field2) and THEN join to t1. That may even be faster than the way you
are doing things now.
So the query would look like:
SELECT t1.field1, t1.field2, t1.field3, a.t2avg FROM t1,
(SELECT field1, avg(field2) as t2avg FROM t2 GROUP BY field1) as a
WHERE t1.field1 = a.field1
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-performance@postgresql.org
Cc: bruno@wolff.to, vitalib@012.net.il
Subject: Re: Additional select fields in a GROUP BY
In-Reply-To: <20040613135212.GA3260@wolff.to>
* 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