Re: Conditional return of aggregated data - Mailing list pgsql-general

From Alban Hertroys
Subject Re: Conditional return of aggregated data
Date
Msg-id CAF-3MvPTKN1n+ewQRhhrE=NjvMmwFptsRdTvx5ZadgmDEx78wg@mail.gmail.com
Whole thread Raw
In response to Conditional return of aggregated data  (Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch>)
List pgsql-general

On Mon, 2 Dec 2019 at 12:11, Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch> wrote:

My initial idea was something along the lines of :
 select (select sum(statcount) from stats_residence) as aggstat,statcount,short_name_en from stats_residence where aggstat>some_number;
 
One option is to move the aggregate to the where-clause. If you also need the value in your select-list, you can just repeat the subselect there, usually the planner is smart enough to figure out that it can just re-use the result.

select short_name_en from stats_residence where (select sum(statcount) from stats_residence) >some_number;

--
If you can't see the forest for the trees,
Cut the trees and you'll see there is no forest.

pgsql-general by date:

Previous
From: Laura Smith
Date:
Subject: Re: Conditional return of aggregated data
Next
From: Tom Lane
Date:
Subject: Re: Connection terminated but client didn't realise