Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ewCHk-000509-SE for pgsql-general@arkaria.postgresql.org; Wed, 14 Mar 2018 19:42:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ewCHj-0007Qd-Gs for pgsql-general@arkaria.postgresql.org; Wed, 14 Mar 2018 19:42:03 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ewCHi-0007QU-V0 for pgsql-general@lists.postgresql.org; Wed, 14 Mar 2018 19:42:03 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ewCHf-0007JX-A6 for pgsql-general@lists.postgresql.org; Wed, 14 Mar 2018 19:42:01 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 4AE3421C24D5 for ; Wed, 14 Mar 2018 22:41:56 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.1.200] (ppp109-252-62-9.pppoe.spdop.ru [109.252.62.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id A54BC21C24C9 for ; Wed, 14 Mar 2018 22:41:55 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1521056515; bh=CgHRxy6/PS7sOr3uaDdni6kam3rxQ9BYNx17I7eQ4aY=; h=Subject:To:References:From:Date:In-Reply-To; b=PQ/SiPPJti9rj8FjrhWR60kfestibwXTBBFmlWjD8uYldyGaEbrUUasmXaD9rRgST nYsin0PShOREh3lA1HwEZrE6hWOtd+WuPyBHhq2FOwKwtdN77rdV7PGZ3QsTr2Rxt8 EyyFb0jO9xn1X3pdf7u+LhD70TUBjiUvhXGBqO5o= Subject: Re: Extract elements from JSON array and return them as concatenated string To: pgsql-general@lists.postgresql.org References: From: "Ivan E. Panchenko" Message-ID: Date: Wed, 14 Mar 2018 22:41:55 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------A23D406430085A5F70C9A831" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------A23D406430085A5F70C9A831 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 14.03.2018 20:02, Alexander Farber пишет: > Thank you, Ivan! I am trying to apply your suggestion to my table - > > On Wed, Mar 14, 2018 at 5:34 PM, Ivan E. Panchenko > > wrote: > > Hi Alex, > > SELECT  string_agg(x->>'letter','') FROM json_array_elements( > > '[{"col": 7, "row": 12, "value": 3, "letter": "A"}, {"col": 8, > "row": 12, "value": 10, "letter": "B"}, {"col": 9, "row": 12, > "value": 1, "letter": "C"}, {"col": 10, "row": 12, "value": 2, > "letter": "D"}]'::json > > ) x; > > > # select * from words_moves where gid=656 order by played desc limit 3; >  mid  | action | gid | uid  | played | > tiles                                                 | score > ------+--------+-----+------+-------------------------------+------------------------------------------------------------------------------------------------------+------- >  1353 | swap   | 656 |    7 | 2018-03-14 17:22:18.430082+01 | "ЙНРР" > |     ¤ >  1352 | play   | 656 | 1199 | 2018-03-14 16:55:45.898379+01 | [{"col": > 4, "row": 7, "value": 2, "letter": "С"}, {"col": 5, "row": 7, "value": > 3, "letter": "У"}]   |    19 >  1351 | play   | 656 |    7 | 2018-03-14 16:38:48.132546+01 | [{"col": > 9, "row": 11, "value": 5, "letter": "Ж"}, {"col": 9, "row": 13, > "value": 2, "letter": "М"}] |    16 > (3 rows) > > by trying the following: > > #  select string_agg(x->>'letter', ' ') from (select > jsonb_array_elements(tiles) from words_moves where gid=656 and > action='play' order by played desc limit 5) x; > ERROR:  42883: operator does not exist: record ->> unknown > LINE 1: select string_agg(x->>'letter', ' ') from (select jsonb_arra... >                            ^ > HINT:  No operator matches the given name and argument type(s). You > might need to add explicit type casts. > LOCATION:  op_error, parse_oper.c:728 > > I am probably missing something obvious? Yes, here x is the alias for the record, not for the json field. So you need to write the query like select string_agg(x->>'letter', ' ') from (    select jsonb_array_elements(tiles) x    from words_moves    where gid=656 and action='play'    order by played desc limit 5 ) y; > > Regards > Alex > > Regards, Ivan --------------A23D406430085A5F70C9A831 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

    
14.03.2018 20:02, Alexander Farber пишет:
Thank you, Ivan! I am trying to apply your suggestion to my table -

On Wed, Mar 14, 2018 at 5:34 PM, Ivan E. Panchenko <i.panchenko@postgrespro.ru> wrote:
Hi Alex,

SELECT  string_agg(x->>'letter','') FROM json_array_elements(

'[{"col": 7, "row": 12, "value": 3, "letter": "A"}, {"col": 8, "row": 12, "value": 10, "letter": "B"}, {"col": 9, "row": 12, "value": 1, "letter": "C"}, {"col": 10, "row": 12, "value": 2, "letter": "D"}]'::json

) x;


# select * from words_moves where gid=656 order by played desc limit 3;
 mid  | action | gid | uid  |            played             |                                                tiles                                                 | score
------+--------+-----+------+-------------------------------+------------------------------------------------------------------------------------------------------+-------
 1353 | swap   | 656 |    7 | 2018-03-14 17:22:18.430082+01 | "ЙНРР"                                                                                               |     ¤
 1352 | play   | 656 | 1199 | 2018-03-14 16:55:45.898379+01 | [{"col": 4, "row": 7, "value": 2, "letter": "С"}, {"col": 5, "row": 7, "value": 3, "letter": "У"}]   |    19
 1351 | play   | 656 |    7 | 2018-03-14 16:38:48.132546+01 | [{"col": 9, "row": 11, "value": 5, "letter": "Ж"}, {"col": 9, "row": 13, "value": 2, "letter": "М"}] |    16
(3 rows)
 
by trying the following:

#  select string_agg(x->>'letter', ' ') from (select jsonb_array_elements(tiles) from words_moves where gid=656 and action='play' order by played desc limit 5) x;
ERROR:  42883: operator does not exist: record ->> unknown
LINE 1: select string_agg(x->>'letter', ' ') from (select jsonb_arra...
                           ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
LOCATION:  op_error, parse_oper.c:728

I am probably missing something obvious?
Yes, here x is the alias for the record, not for the json field. So you need to write the query like

select string_agg(x->>'letter', ' ')
from (
   select jsonb_array_elements(tiles) x
   from words_moves
   where gid=656 and action='play'
   order by played desc limit 5
) y;



Regards
Alex


Regards,
Ivan
--------------A23D406430085A5F70C9A831--