Re: Conditional return of aggregated data

From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: Laura Smith <n5d9xq3ti233xiyif2vp(at)protonmail(dot)ch>
Cc: postgre <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Conditional return of aggregated data
Date: 2019-12-02 12:08:01
Message-ID: CAF-3MvPTKN1n+ewQRhhrE=NjvMmwFptsRdTvx5ZadgmDEx78wg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, 2 Dec 2019 at 12:11, Laura Smith <n5d9xq3ti233xiyif2vp(at)protonmail(dot)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.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2019-12-02 15:17:04 Re: Connection terminated but client didn't realise
Previous Message Laura Smith 2019-12-02 12:05:33 Re: Conditional return of aggregated data