Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1opdSF-0006Kv-Nf for pgsql-sql@arkaria.postgresql.org; Mon, 31 Oct 2022 22:40:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1opdSE-00069r-IC for pgsql-sql@arkaria.postgresql.org; Mon, 31 Oct 2022 22:40:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1opdSE-00068o-7h for pgsql-sql@lists.postgresql.org; Mon, 31 Oct 2022 22:40:26 +0000 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1opdSB-00087k-RJ for pgsql-sql@lists.postgresql.org; Mon, 31 Oct 2022 22:40:25 +0000 Received: by mail-pj1-x1035.google.com with SMTP id k5so4039730pjo.5 for ; Mon, 31 Oct 2022 15:40:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to; bh=xmPvKG1Y8Znt/2sMhj8LE5KiZ+eVoF62bBl8ZAhx6mM=; b=odFtpQpDHuHkv4WB8lbSLeVavz7iBI2dWSyXk9NHmoj9YZfX+Pjgs8jABxx9gctHY0 btEfzIjcRkZwGtzaG032k5FpibHIS1oiCqX4eRMOsrDo3aOQgBsBIbedeICLZrhrglUj yFl3p+XftDRbml4qEhOIt8ycUmgzwcIQ6NjeogZ7hmg5kYf77SGjf6253CD6fIOhr2Af YRqhpS8ll/V46jm5rNe1ABynM2kLGhCZmlQCTqvtK054mWpQvW7hR13jaWPgufIqXhzB tbIYcCCIBUpwAAkeE4KN9eZswr3aSc438I3itseRpPxaklrd9cttNhc+fuy8zJPcKyCL K/6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=xmPvKG1Y8Znt/2sMhj8LE5KiZ+eVoF62bBl8ZAhx6mM=; b=Ks4RDVvTd9zhnNhxe09vNYINEVgW89ONdyk8QN7CBN3CxssxpclX83AmDwUR03cL9X DY66qI3q2TTKE7UZnwBCg87i/f888Ds1iNGBY8UT/V60WDfZPuNWgeW2Ho7mPEhaivnQ 6S7hqGokM1IsF3YnXV/X+dNobGZkw+fM2XWSvddisAV0xmfstwYVCDWmlr0HPtEganPJ 6iGwTV8ea5TbXIHLrLk/AAmq3mu8prfpP10nOmZb1w+2A9CHs6acmrgvrdRhbD1dPRBS Py40kSEtZHyBR17qIvzLHkoLFLMaYyBMj4s0NkFSESZ2+LBa8EwQJhCDj+YplNX8MazG /ifw== X-Gm-Message-State: ACrzQf0EeiU+Ql8uXZELt5NU7dih1+ti2DMvwcal+tS1eiqb36J+Fn8l jEDBG4G8rtJ5IMf7+PBiKEdQHi6GBhc= X-Google-Smtp-Source: AMsMyM6hhuebDcl+OLTHOrT+RV92IjII+ar3HiUSgocsTT2107wBUL9iaaT5GB7+3wOZg1rC5mov5w== X-Received: by 2002:a17:902:c94f:b0:173:3def:265 with SMTP id i15-20020a170902c94f00b001733def0265mr20145pla.30.1667256022560; Mon, 31 Oct 2022 15:40:22 -0700 (PDT) Received: from [10.128.56.136] ([155.98.131.0]) by smtp.gmail.com with ESMTPSA id k125-20020a632483000000b004639c772878sm4668961pgk.48.2022.10.31.15.40.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 31 Oct 2022 15:40:22 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------YFN4F3rTxif0JvMgQf28yLPq" Message-ID: Date: Mon, 31 Oct 2022 16:40:20 -0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Subject: Re: access sub elements using any() Content-Language: en-CA To: Torsten Grust , pgsql-sql References: <5fb866d1-8d2f-5118-18e2-9bbf227cbf1a@gmail.com> <31612ff3-67a4-4781-b359-0d182f73fe84@betaapp.fastmail.com> From: Rob Sargent In-Reply-To: <31612ff3-67a4-4781-b359-0d182f73fe84@betaapp.fastmail.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------YFN4F3rTxif0JvMgQf28yLPq Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 10/31/22 16:36, Torsten Grust wrote: > Hi, > > On Mon, Oct 31, 2022, at 18:04, Rob Sargent wrote: >> 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?  Say I wanted just those with 2 >> as first element.  "where 2 = any(v[1])" does not work and not sure >> it's supported.   And I cannot craft a lhs to fussy-match each of the >> elements in v.  Can this be done? > > if your inner arrays would be row values instead, i.e. if v would read > >   v = array[(1,'a'),(2,'b'),(3,'c'),(2,'d')] :: t[] >   assuming CREATE TYPE t AS (x int, y text); > > then your query could simply be > >   SELECT r.* >   FROM   unnest(v) AS r >   WHERE  r.x = 2; > > Cheers, >   —Torsten > > -- > | Torsten Grust > | teggy@fastmail.com > > Thank you! --------------YFN4F3rTxif0JvMgQf28yLPq Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 10/31/22 16:36, Torsten Grust wrote:
Hi,

On Mon, Oct 31, 2022, at 18:04, Rob Sargent wrote:
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?  Say I wanted just those with 2 as first element.  "where 2 = any(v[1])" does not work and not sure it's supported.   And I cannot craft a lhs to fussy-match each of the elements in v.  Can this be done?

if your inner arrays would be row values instead, i.e. if v would read

  v = array[(1,'a'),(2,'b'),(3,'c'),(2,'d')] :: t[]
  assuming CREATE TYPE t AS (x int, y text);

then your query could simply be

  SELECT r.*
  FROM   unnest(v) AS r
  WHERE  r.x = 2;

Cheers,
  —Torsten

--
| Torsten Grust


Thank you!

--------------YFN4F3rTxif0JvMgQf28yLPq--