Re: total and partial sums in the same query?? - Mailing list pgsql-sql

From David G. Johnston
Subject Re: total and partial sums in the same query??
Date
Msg-id CAKFQuwYQyHHgtFOhZ7PpECPLO61LANtt7WdYng=SmvLBDKCubg@mail.gmail.com
Whole thread Raw
In response to Re: total and partial sums in the same query??  (Iuri Sampaio <iuri.sampaio@gmail.com>)
List pgsql-sql
On Sat, Oct 10, 2020 at 8:28 AM Iuri Sampaio <iuri.sampaio@gmail.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.

pgsql-sql by date:

Previous
From: Iuri Sampaio
Date:
Subject: Re: total and partial sums in the same query??
Next
From: Martin Norbäck Olivers
Date:
Subject: get counts of multiple field values in a jsonb column