pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Alexander Farber <alexander.farber@gmail.com>
To: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: UPDATE from CTE syntax error
Date: Sat, 26 May 2018 09:37:22 -0700
Message-ID: <ca819b5d-0cbf-0c72-2e96-99561c624fda@aklaver.com> (raw)
In-Reply-To: <CAADeyWgGcDe652eet9cTKdcycLfym8GL7u4fxQf4Ye+BzuHzdQ@mail.gmail.com>
References: <CAADeyWgGcDe652eet9cTKdcycLfym8GL7u4fxQf4Ye+BzuHzdQ@mail.gmail.com>

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




view thread (4+ messages)  latest in thread

Message-ID: <ca819b5d-0cbf-0c72-2e96-99561c624fda@aklaver.com>
Permalink:  ../ca819b5d-0cbf-0c72-2e96-99561c624fda@aklaver.com/
Also on:    postgresql.org/message-id/ca819b5d-0cbf-0c72-2e96-99561c624fda@aklaver.com

 · 

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: adrian.klaver@aklaver.com, alexander.farber@gmail.com
  Subject: Re: UPDATE from CTE syntax error
  In-Reply-To: <ca819b5d-0cbf-0c72-2e96-99561c624fda@aklaver.com>
  References: <CAADeyWgGcDe652eet9cTKdcycLfym8GL7u4fxQf4Ye+BzuHzdQ@mail.gmail.com> <ca819b5d-0cbf-0c72-2e96-99561c624fda@aklaver.com>

* 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