Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lCKNf-00073Q-Pz for pgsql-general@arkaria.postgresql.org; Wed, 17 Feb 2021 10:48:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lCKNe-00072G-OO for pgsql-general@arkaria.postgresql.org; Wed, 17 Feb 2021 10:48:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lCKNe-000729-Ge for pgsql-general@lists.postgresql.org; Wed, 17 Feb 2021 10:48:26 +0000 Received: from mail-pg1-x536.google.com ([2607:f8b0:4864:20::536]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lCKNb-0002qp-5H for pgsql-general@postgresql.org; Wed, 17 Feb 2021 10:48:26 +0000 Received: by mail-pg1-x536.google.com with SMTP id p21so2582995pgl.12 for ; Wed, 17 Feb 2021 02:48:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=DXyBy4l5P6XeFVTMyNG9Uw8oUxeobB4Ieo8YFoxBMq0=; b=iYn8oB6RQSKBUuYL773uTSfRusFrMaf9+jd/LAxQyghmULJD83p42m1ToIZqOIkfrI /0bAUujvOP3A3NUS5XEG0s8HRLuLJeNQl8yqaq2HPfNPvzcYEVm4699aO0pLX3UDWAHn bmjDTf16KnJnBouo3AY4M+qIAn0JkH7MQC/q2g6KD2VzLpYRsNJwwyTapdakFx7uC79E kW3OvuiDZNoF7dU9Oa0Jn8PztnQF9taJqcRSW+DZkjF+CXHQcGfVPQM3Y9tzdSB15qeH 97WX8TlFlZ/hDXgo6fkSczwMfXa1IQtH2l7FK1NtiWug5sv+EHjhm5lPVmdqjU8lDCzh Aijg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=DXyBy4l5P6XeFVTMyNG9Uw8oUxeobB4Ieo8YFoxBMq0=; b=dhA2LrS2pOd+dkqx6vNuxPSfveDObRQFdbAGHfwfzWMqSp+7rL7yOEvIZ+RLWuPmQP QH+oBWa/lx2qFu0ooByfHhPFp7ppEO+WHtUjJYZBorFcZUzpWNZWyr6tRfCsQyhiGjU0 C6L6Pb3CQQVnnEGj1sZrbZOQa8toToD8qnf1eaG0jix5TA64eggGRWmCem8G+VxfS8Rz nPFFoMann4VtpuV8XeZPegQ54ejRh1usZjlIPg0rAa2Kt1jOvSepKbC2HYpwprhH4y0p u6dKWAsnFnQomsnw35L2zuJWmSj52L0Tn4g+XuJtm5kr5Lo1VBp9nH+WMlSCu3FpxI9M LM/w== X-Gm-Message-State: AOAM530HEBucpTjZuZ3OW7kZ9RvryicOMalawCUwsaKDBL9f3P87rKJ+ M7knTIttcNzUz3lyP6X8jDBRy/TGMGhOpop3fv8= X-Google-Smtp-Source: ABdhPJyJya1YerPcJXnY6vwmQ1HBkYJB6yeJXfbViv5ZUWdWuBrq2N9S1wqTIi8nYgsRM0yBhj1au8g+Pg52PZP5BwU= X-Received: by 2002:a63:c0a:: with SMTP id b10mr5490583pgl.251.1613558901087; Wed, 17 Feb 2021 02:48:21 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Pavel Stehule Date: Wed, 17 Feb 2021 11:47:44 +0100 Message-ID: Subject: Re: How to return a jsonb list of lists (with integers) To: Alexander Farber Cc: pgsql-general Content-Type: multipart/alternative; boundary="0000000000001fa07805bb85f5bf" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --0000000000001fa07805bb85f5bf Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi st 17. 2. 2021 v 11:40 odes=C3=ADlatel Alexander Farber < alexander.farber@gmail.com> napsal: > I have tried the following, but IF FOUND is always false for some reason: > > _versus :=3D JSONB_BUILD_ARRAY( > SUM(CASE WHEN (player1 =3D in_uid AND state1 =3D > 'won') OR (player2 =3D in_uid AND state2 =3D 'won') THEN 1 ELSE 0 > END)::integer, > SUM(CASE WHEN (player1 =3D in_uid AND state1 =3D > 'lost') OR (player2 =3D in_uid AND state2 =3D 'lost') THEN 1 ELSE 0 > END)::integer, > SUM(CASE WHEN (player1 =3D in_uid AND state1 =3D > 'draw') OR (player2 =3D in_uid AND state2 =3D 'draw') THEN 1 ELSE 0 > END)::integer > ) > FROM words_games > WHERE finished IS NOT NULL > AND ( > (player1 =3D in_uid AND player2 =3D in_opponent) = OR > (player2 =3D in_uid AND player1 =3D in_opponent) > ); > Don't do this. When you want to use a query, then use SELECT INTO. Regards Pavel > IF FOUND THEN > out_data :=3D JSONB_INSERT(out_data, '{versus}', > _versus); > END IF; > > --0000000000001fa07805bb85f5bf Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi

st 17. 2. 2021 v=C2=A011:40 odes=C3=ADlatel A= lexander Farber <alexander.farber@gmail.com> napsal:
I hav= e tried the following, but IF FOUND is always false for some reason:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 _versus :=3D = JSONB_BUILD_ARRAY(
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 SUM(CASE WHEN (player1 =3D in_uid= AND state1 =3D 'won')=C2=A0 OR (player2 =3D in_uid AND state2 =3D = 'won')=C2=A0 THEN 1 ELSE 0 END)::integer,
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 SUM(C= ASE WHEN (player1 =3D in_uid AND state1 =3D 'lost') OR (player2 =3D= in_uid AND state2 =3D 'lost') THEN 1 ELSE 0 END)::integer,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 SUM(CASE WHEN (player1 =3D in_uid AND state1 =3D 'draw= 9;) OR (player2 =3D in_uid AND state2 =3D 'draw') THEN 1 ELSE 0 END= )::integer
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 )
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FRO= M words_games
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 WHERE finished IS NOT NULL
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 AND (
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (player1 =3D in_uid AND p= layer2 =3D in_opponent) OR
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (player2 =3D in_uid AND playe= r1 =3D in_opponent)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 );

Don't do thi= s. When you want to use a query, then use SELECT INTO.

Regards

Pavel



=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 IF FOUND THEN
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 out_data :=3D JSONB_INSERT(out_da= ta, '{versus}', _versus);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 END IF;

--0000000000001fa07805bb85f5bf--