Re: total and partial sums in the same query??

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Iuri Sampaio <iuri(dot)sampaio(at)gmail(dot)com>
Cc: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: total and partial sums in the same query??
Date: 2020-10-10 17:55:20
Message-ID: CAKFQuwYQyHHgtFOhZ7PpECPLO61LANtt7WdYng=SmvLBDKCubg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sat, Oct 10, 2020 at 8:28 AM Iuri Sampaio <iuri(dot)sampaio(at)gmail(dot)com> wrote:

> So, feel free whether to go deeper continuing this discussion
>

Specifically:

WITH v_normalized AS (SELECT split_part(description, ' ', 25) AS type1,
EXTRACT(MONTH FROM creation_date) AS month FROM qt_vehicle_ti)
SELECT type1, count(*) as count_total, count(*) filter (where month=10) AS
count_m10 FROM v_normalized GROUP BY type1;

There is no benefit to having a join here.

Using WITH to compute v_normalized in just this query works but I suspect
you will find you are writing something like that a lot which suggests it,
or rather something similar but a bit more general, should be computed
using CREATE VIEW.

David J.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Martin Norbäck Olivers 2020-10-17 15:00:05 get counts of multiple field values in a jsonb column
Previous Message Iuri Sampaio 2020-10-10 15:28:20 Re: total and partial sums in the same query??