pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <shammat@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: JSONB_AGG: aggregate function calls cannot be nested
Date: Sat, 20 Feb 2021 21:01:23 +0100
Message-ID: <1a210dd1-74b3-d162-3579-ba6e79a72be3@gmx.net> (raw)
In-Reply-To: <CAADeyWhzYAXxuBJaQA2n-wuK1Uoc2FbKAk+iE2G1Mcg-vD0r0g@mail.gmail.com>
References: <CAADeyWhzYAXxuBJaQA2n-wuK1Uoc2FbKAk+iE2G1Mcg-vD0r0g@mail.gmail.com>

Alexander Farber schrieb am 20.02.2021 um 19:39:
> So I am trying:
>
> # SELECT
>                  JSONB_AGG(TO_CHAR(finished, 'YYYY-MM-DD')) AS day,
>                  JSONB_AGG(SUM(CASE WHEN reason='regular' or reason='resigned' THEN 1 ELSE 0 END)::int) AS completed,
>                  JSONB_AGG(SUM(CASE WHEN reason='expired' THEN 1 ELSE 0 END)::int) AS expired
>          FROM words_games
>          WHERE finished > CURRENT_TIMESTAMP - INTERVAL '2 week'
>          GROUP BY day
>          ORDER BY day;
> ERROR:  aggregate function calls cannot be nested
> LINE 3:                 JSONB_AGG(SUM(CASE WHEN reason='regular' or ...

You need a second level of grouping:

     select day as day,
            jsonb_agg(completed) as completed,
            jsonb_agg(expired) as expired)
     from (
       SELECT TO_CHAR(finished, 'YYYY-MM-DD') AS day,
              SUM(CASE WHEN reason='regular' or reason='resigned' THEN 1 ELSE 0 END)::int AS completed,
              SUM(CASE WHEN reason='expired' THEN 1 ELSE 0 END)::int) AS expired
       FROM words_games
       WHERE finished > CURRENT_TIMESTAMP - INTERVAL '2 week'
       GROUP BY day
     ) t
     GROUP BY day
     ORDER BY day;


Btw:

     SUM(CASE WHEN reason='regular' or reason='resigned' THEN 1 ELSE 0 END)::int AS completed,

can also be written as

     count(*) filter (where reason in ('regular', 'resigned') as completed






view thread (10+ messages)  latest in thread

Message-ID: <1a210dd1-74b3-d162-3579-ba6e79a72be3@gmx.net>
Permalink:  ../1a210dd1-74b3-d162-3579-ba6e79a72be3@gmx.net/
Also on:    postgresql.org/message-id/1a210dd1-74b3-d162-3579-ba6e79a72be3@gmx.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: shammat@gmx.net
  Subject: Re: JSONB_AGG: aggregate function calls cannot be nested
  In-Reply-To: <1a210dd1-74b3-d162-3579-ba6e79a72be3@gmx.net>
  References: <CAADeyWhzYAXxuBJaQA2n-wuK1Uoc2FbKAk+iE2G1Mcg-vD0r0g@mail.gmail.com> <1a210dd1-74b3-d162-3579-ba6e79a72be3@gmx.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox