Re: Avoiding double-counting in aggregates with more than one join?

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Paul Jungwirth <pj(at)illuminatedcomputing(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Avoiding double-counting in aggregates with more than one join?
Date: 2016-11-18 18:42:03
Message-ID: CAKFQuwb2o5adN0dartbg-_1e6DY2yC7oy9h3EvyEyDa4=7OLGw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Nov 18, 2016 at 10:16 AM, Paul Jungwirth <
pj(at)illuminatedcomputing(dot)com> wrote:

> But is there a better way?

​Nothing that would be more readable nor likely more performant.

When performing aggregation it is necessary to limit the scope of the query
to only whatever it is you are calculating. Since you wish to compute two
things you need two separate parts ​plus a third to combine them.

​If performance is a concern you should move the aggregation queries
directly to the main query instead of using the optimization fencing CTE.

SELECT
FROM products
LEFT JOIN (
SELECT sum()
)​ s USING (product_id)
LEFT JOIN (
SELECT sum()
) r USING (product_id)

​If the second "scope" doesn't need to be calculated but simply informs the
one-and-only aggregate you should use SEMI JOIN (EXISTS) instead of a
INNER/LEFT JOIN​. But that isn't what you have here.

David J.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Man Trieu 2016-11-19 05:46:47 How to change order sort of table in HashJoin
Previous Message Paul Jungwirth 2016-11-18 17:16:23 Avoiding double-counting in aggregates with more than one join?