From: | Bruno Wolff III <bruno(at)wolff(dot)to> |
---|---|
To: | Vitaly Belman <vitalib(at)012(dot)net(dot)il> |
Cc: | pgsql-performance(at)postgresql(dot)org |
Subject: | Re: Additional select fields in a GROUP BY |
Date: | 2004-06-13 13:52:12 |
Message-ID: | 20040613135212.GA3260@wolff.to |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-performance |
On Sun, Jun 13, 2004 at 06:21:17 +0300,
Vitaly Belman <vitalib(at)012(dot)net(dot)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
From | Date | Subject | |
---|---|---|---|
Next Message | Vitaly Belman | 2004-06-13 17:35:36 | Re: Additional select fields in a GROUP BY |
Previous Message | Tom Lane | 2004-06-13 03:39:55 | Re: Additional select fields in a GROUP BY |