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 1kmUQL-000466-7D for pgsql-general@arkaria.postgresql.org; Tue, 08 Dec 2020 04:16:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kmUQK-000848-1P for pgsql-general@arkaria.postgresql.org; Tue, 08 Dec 2020 04:16:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmUQJ-000841-Q9 for pgsql-general@lists.postgresql.org; Tue, 08 Dec 2020 04:16:23 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmUQG-0002c3-Vt for pgsql-general@lists.postgresql.org; Tue, 08 Dec 2020 04:16:22 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 0B84GI4R3210188; Mon, 7 Dec 2020 23:16:18 -0500 From: Tom Lane To: Ken Tanzer cc: Steve Baldwin , "David G. Johnston" , Rob Sargent , "pgsql-generallists.postgresql.org" Subject: Re: Extracting data from jsonb array? In-reply-to: References: <72f54672-ad66-0370-9128-206e49389b60@gmail.com> Comments: In-reply-to Ken Tanzer message dated "Mon, 07 Dec 2020 19:57:06 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3210186.1607400978.1@sss.pgh.pa.us> Date: Mon, 07 Dec 2020 23:16:18 -0500 Message-ID: <3210187.1607400978@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Ken Tanzer writes: >> => select f.id, f.f1,f.f2,array_agg(t.key2) from foo as f, >> jsonb_to_recordset(js) as t(key2 text) group by f.id; > After a little more thought and experimenting, I'm not so sure about this > part. In particular, I'm not clear why Postgres isn't complaining about > the f1 and f2 fields. (It's not giving the "must appear in the GROUP BY > clause or be used in an aggregate function" error that I would expect, and > that I am getting when I try to apply this to my real query.) > Can anyone explain to me why those fields don't need to be grouped? Thanks. If foo.id is a primary key, it knows that the "group by" doesn't really merge any rows of foo, so it lets you get away with that. I think this is actually required by spec, but am too lazy to go check right now. If foo.id isn't a primary key, then I'm confused too. Can we see the full declaration of the table? regards, tom lane