From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: David Salisbury <dsalis@ucar.edu>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Alban Hertroys <haramrae@gmail.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Arrays and ANY problem
Date: Mon, 30 Sep 2019 19:55:00 +0100
Message-ID: <875zl9mxhz.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <CAKXTjV=J9VWud7pakt_s8u6V2J9v5BZCoR8kpX80GQYYy5m7MA@mail.gmail.com>
References: <CAKXTjVmV+mHXvzCKtKuHeooYYObNQhST7eG7kJmUcJy7=1cqTg@mail.gmail.com>
<775E048E-4316-4AFE-B4A7-A25AC79DE002@gmail.com>
<526FB575-7D50-4D11-A053-8B4B66D5200F@gmail.com>
<28608.1569451105@sss.pgh.pa.us>
<CAKXTjV=J9VWud7pakt_s8u6V2J9v5BZCoR8kpX80GQYYy5m7MA@mail.gmail.com>
>>>>> "David" == David Salisbury <dsalis@ucar.edu> 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)
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: andrew@tao11.riddles.org.uk, dsalis@ucar.edu, tgl@sss.pgh.pa.us, haramrae@gmail.com
Subject: Re: Arrays and ANY problem
In-Reply-To: <875zl9mxhz.fsf@news-spur.riddles.org.uk>
References: <CAKXTjVmV+mHXvzCKtKuHeooYYObNQhST7eG7kJmUcJy7=1cqTg@mail.gmail.com> <775E048E-4316-4AFE-B4A7-A25AC79DE002@gmail.com> <526FB575-7D50-4D11-A053-8B4B66D5200F@gmail.com> <28608.1569451105@sss.pgh.pa.us> <CAKXTjV=J9VWud7pakt_s8u6V2J9v5BZCoR8kpX80GQYYy5m7MA@mail.gmail.com> <875zl9mxhz.fsf@news-spur.riddles.org.uk>
* 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