Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iF0pE-0003lK-BA for pgsql-general@arkaria.postgresql.org; Mon, 30 Sep 2019 18:55:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iF0pB-0001LW-9J for pgsql-general@arkaria.postgresql.org; Mon, 30 Sep 2019 18:55:09 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1iF0pA-0001LH-VT for pgsql-general@lists.postgresql.org; Mon, 30 Sep 2019 18:55:09 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iF0p8-0007KX-8A for pgsql-general@postgresql.org; Mon, 30 Sep 2019 18:55:08 +0000 Received: from [192.168.127.1] (port=33460 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtp (Exim 4.92.2 (FreeBSD)) (envelope-from ) id 1iF0p2-000EMu-IO; Mon, 30 Sep 2019 18:55:00 +0000 Received: from localhost ([127.0.0.1]:38574 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.92.1 (FreeBSD)) (envelope-from ) id 1iF0p2-000CUb-Ds; Mon, 30 Sep 2019 18:55:00 +0000 From: Andrew Gierth To: David Salisbury Cc: Tom Lane , Alban Hertroys , pgsql-general Subject: Re: Arrays and ANY problem In-Reply-To: (David Salisbury's message of "Mon, 30 Sep 2019 12:23:27 -0600") Message-ID: <875zl9mxhz.fsf@news-spur.riddles.org.uk> References: <775E048E-4316-4AFE-B4A7-A25AC79DE002@gmail.com> <526FB575-7D50-4D11-A053-8B4B66D5200F@gmail.com> <28608.1569451105@sss.pgh.pa.us> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (berkeley-unix) Date: Mon, 30 Sep 2019 19:55:00 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "David" == David Salisbury writes: David> I didn't specify the real problem as it's all wrapped up in David> layers and I didn't want to post a "can someone write the query David> for me". The real problem was I have a table with a string David> holding comma separated numbers, and needed to go to a lookup David> table and replace each of those numbers with it's correlated David> value. So '12,2,10' gets converted to 'twelve,two,ten'. David> Tom's "I'd suggest that making his sub-select return a rowset David> result rather than an array" was spot on and lead me to David> "unnest". For my posted problem this was the simple solution. David> Sorry to narrow things down to my specific array method. David> select name from table_name_ds_tmp where categoryid = ANY ( select David> unnest(string_to_array( '200,400', ',')::bigint[]) ); This won't necessarily preserve the order of elements (it might sometimes look like it does, but it's fooling you). It also won't handle duplicate numbers. The right solution that does preserve order would be: select name from unnest(string_to_array( '200,400', ',')::bigint[]) with ordinality as u(id,ord) join table_name_ds_tmp t on (t.category_id=u.id) order by u.ord; (wrap an ARRAY( ) around that if you need the result as a single array rather than as rows, or use string_agg(name, ',' order by ord) if you want a comma-separated string result) regexp_split_to_table might be a better method than unnest/string_to_array. -- Andrew (irc:RhodiumToad)