Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fMcCE-0007MV-GO for pgsql-general@arkaria.postgresql.org; Sat, 26 May 2018 16:37:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fMcCD-0003fL-0i for pgsql-general@arkaria.postgresql.org; Sat, 26 May 2018 16:37:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fMcCB-0003fB-6o for pgsql-general@lists.postgresql.org; Sat, 26 May 2018 16:37:32 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fMcC6-000246-9N for pgsql-general@postgresql.org; Sat, 26 May 2018 16:37:30 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 51DE221763; Sat, 26 May 2018 12:37:24 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Sat, 26 May 2018 12:37:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=A5Pf6uMpYvHW375iYr6e4c6Nx1Xx4 w53/Eh3uLqx89U=; b=PUVHL3he/aAdXD4hdEFzd4ygJl0QQlnO499E5vK2Cd2N9 GZz+rvCkVhhzBuXOGFJ0J/g4NK8UCBAkgzu26Ex0roxJzOyO53bE02FGz2IMPr7K Wum4Up63aOTrcL1WBBY4goBcLK/o5YIbMcJj7lCGP8vGeH8NSpzJPjs5ipY7bnSg xbIydR+YZIhX9a9XuZURkhgV1UvWoozn6ioT4Z7zwMakaaE0WTd5jUsLs9RRpXXX pxhq4R4PdZZzEg0mvbO0iCsBYFxOHWPCxwPRnO6CBwJw46n/SzCzAoRFe+A2+pvG Dj9jGWMjiIND66x2FsUHGb2CRmyz/odPcF5AsuB7A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=A5Pf6u MpYvHW375iYr6e4c6Nx1Xx4w53/Eh3uLqx89U=; b=HL1cSijCipfv1RamIjZAw9 YNn+4tKQBs9frFTZ4eljXb7aYfXtUXXVCgKnfh/obedo9r4pH6ZuuX9fOgYIrUMz mZwq73ePyHDKr359l3rVaAC4sDQkVontx+lm69FyCMwWK8N1YCTCi4zPHY4OBoBr vhfL8s6Lkw1M1vKydPe0JNDGgs3ZJKjeCGmmrS+AqeFpGG/ybH/aqmm73U4ev+pl LKbJyGxCMBFovKWWaD6p3xB+g6qHXm/zYqEZmPA61iF+YSoZwca4zW6yjAKRHtKv qLOIYzF0Tel169aaK90atnr0cY6EqcPA/LD3VppZo5+FREBTFQE9G4o/Sv/AiBnQ == X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.2] (unknown [71.212.135.74]) by mail.messagingengine.com (Postfix) with ESMTPA id B0E81E4434; Sat, 26 May 2018 12:37:23 -0400 (EDT) Subject: Re: UPDATE from CTE syntax error To: Alexander Farber , pgsql-general References: From: Adrian Klaver Message-ID: Date: Sat, 26 May 2018 09:37:22 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 05/26/2018 09:21 AM, Alexander Farber wrote: > Good evening, > > I am struggling with the syntax, please help. > > This query with a CTE works ok: > > WITH > >              extract_letters AS ( > >                                    SELECT > >                                                          mid, > > >  STRING_AGG(x->>'letter', '') AS letters > >         FROM ( > >                                SELECT > >                                                      mid, > > >  JSONB_ARRAY_ELEMENTS(tiles) AS x > >        FROM words_moves > >                      WHERE action='play' > >                             ) z > > GROUP BY mid) > >       SELECT * from extract_letters; > >  mid   | letters > --------+--------- >      12 | АКЖОЛ >      15 | ДМО >      16 | ТО >      20 | ШГА >      21 | КТИ >      22 | ВОЗ >      24 | АКПОНК > > But UPDATEing from the CTE does not - > > WITH extract_letters AS ( > >                       SELECT > >                                              mid, > > >  STRING_AGG(x->>'letter', '') AS letters > >         FROM ( > >                                SELECT > >                                                      mid, > > >  JSONB_ARRAY_ELEMENTS(tiles) AS x > >        FROM words_moves > >                      WHERE action='play' > >                             ) z > >                                   GROUP BY mid) > >          UPDATE words_moves m > >                        SET m.letters = el.letters I would say the above is the problem: https://www.postgresql.org/docs/10/static/sql-update.html "column_name The name of a column in the table named by table_name. The column name can be qualified with a subfield name or array subscript, if needed. Do not include the table's name in the specification of a target column — for example, UPDATE table_name SET table_name.col = 1 is invalid. " So it should be: SET letters = el.letters > >                                      FROM extract_letters el > >                                            WHERE m.mid = el.mid; > > ERROR:  42703: column "m" of relation "words_moves" does not exist > LINE 14: SET m.letters = el.letters >              ^ > > Regards > Alex -- Adrian Klaver adrian.klaver@aklaver.com