Re: get counts of multiple field values in a jsonb column

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Martin Norbäck Olivers <martin(at)norpan(dot)org>
Cc: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: Re: get counts of multiple field values in a jsonb column
Date: 2020-10-17 15:38:50
Message-ID: CAKFQuwZKkv3ZLOfmipU-8JRLKTxhQ4ThFwDJQrX45Fk_MoXUiA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sat, Oct 17, 2020 at 8:00 AM Martin Norbäck Olivers <martin(at)norpan(dot)org>
wrote:

> I'm looking at all the aggregation functions but can't quite find one that
> suits this purpose.
>
> I would love to get some input on ways to make this faster.
>

If you want counts the count function is your goto aggregate function.
What you are missing is performing conditional counting.

SELECT fld, count(*) FILTER (WHERE expression) FROM query GROUP BY fld;

https://www.postgresql.org/docs/13/sql-expressions.html#SYNTAX-AGGREGATES

David J.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Rob Sargent 2020-10-17 16:15:12 Re: get counts of multiple field values in a jsonb column
Previous Message Martin Norbäck Olivers 2020-10-17 15:00:05 get counts of multiple field values in a jsonb column