public inbox for [email protected]  
help / color / mirror / Atom feed
From: Tom Lane <[email protected]>
To: Rob Sargent <[email protected]>
Cc: pgsql-sql <[email protected]>
Subject: Re: access sub elements using any()
Date: Mon, 31 Oct 2022 20:50:06 -0400
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>

Rob Sargent <[email protected]> writes:
> Given an array of arrays ( e.g. v = {{1,'a'},{2,'b'},{3,'c'},{2,'d'}} ) 
> is it possible, in plain sql, to access the first element of the listed 
> arrays using the IN function?

If it's actually a 2-D array, and not an array-of-composite which is
what your sample data seems to suggest, then array slicing might help:

regression=# select ('{{1,2},{3,4},{5,6}}'::int[])[:][1];
     int4      
---------------
 {{1},{3},{5}}
(1 row)

regression=# select 2 = any (('{{1,2},{3,4},{5,6}}'::int[])[:][1]);
 ?column? 
----------
 f
(1 row)

regression=# select 3 = any (('{{1,2},{3,4},{5,6}}'::int[])[:][1]);
 ?column? 
----------
 t
(1 row)

			regards, tom lane






view thread (4+ messages)

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 using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: access sub elements using any()
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox