pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
UPDATE from CTE syntax error
4+ messages / 2 participants
[nested] [flat]

* UPDATE from CTE syntax error
@ 2018-05-26 16:21 Alexander Farber <alexander.farber@gmail.com>
  2018-05-26 16:22 ` Re: UPDATE from CTE syntax error Alexander Farber <alexander.farber@gmail.com>
  2018-05-26 16:37 ` Re: UPDATE from CTE syntax error Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Alexander Farber @ 2018-05-26 16:21 UTC (permalink / raw)
  To: pgsql-general

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
                                                                   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

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: UPDATE from CTE syntax error
  2018-05-26 16:21 UPDATE from CTE syntax error Alexander Farber <alexander.farber@gmail.com>
@ 2018-05-26 16:22 ` Alexander Farber <alexander.farber@gmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Alexander Farber @ 2018-05-26 16:22 UTC (permalink / raw)
  To: pgsql-general

Oops, I am sorry for the formatting - Mac + Terminal + Gmail :-/

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: UPDATE from CTE syntax error
  2018-05-26 16:21 UPDATE from CTE syntax error Alexander Farber <alexander.farber@gmail.com>
@ 2018-05-26 16:37 ` Adrian Klaver <adrian.klaver@aklaver.com>
  2018-05-27 14:00   ` Re: UPDATE from CTE syntax error Alexander Farber <alexander.farber@gmail.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Adrian Klaver @ 2018-05-26 16:37 UTC (permalink / raw)
  To: Alexander Farber <alexander.farber@gmail.com>; pgsql-general

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




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: UPDATE from CTE syntax error
  2018-05-26 16:21 UPDATE from CTE syntax error Alexander Farber <alexander.farber@gmail.com>
  2018-05-26 16:37 ` Re: UPDATE from CTE syntax error Adrian Klaver <adrian.klaver@aklaver.com>
@ 2018-05-27 14:00   ` Alexander Farber <alexander.farber@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Alexander Farber @ 2018-05-27 14:00 UTC (permalink / raw)
  To: ; +Cc: pgsql-general

On Sat, May 26, 2018 at 6:37 PM, Adrian Klaver <adrian.klaver@aklaver.com>
wrote:
>
>
> 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
>
>
Thank you Adrian, this has worked

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2018-05-27 14:00 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-05-26 16:21 UPDATE from CTE syntax error Alexander Farber <alexander.farber@gmail.com>
2018-05-26 16:22 ` Alexander Farber <alexander.farber@gmail.com>
2018-05-26 16:37 ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-05-27 14:00   ` Alexander Farber <alexander.farber@gmail.com>

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