Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7509791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:01:36+00:00 2026-05-29T23:01:36+00:00

Here’s my problem: I have a table with 4 columns (type = array, one-dimension)

  • 0

Here’s my problem:

I have a table with 4 columns (type = array, one-dimension) with values like:

   ID        c1             c2           c3

   1      {1, 2, 3}    {10, 20, 30}   {100, 200, 300}
   2      {4, 5, 6}    {11, 22, 33}   {111, 222, 333}

I was wondering if it is possible to get a result like the following:

   select ID, c1, c2, c3 from t1 where c3 = 200 (<---- pseudo-statement!)

Result:

   1, 2, 20, 200

or

   select ID, c1, c2, c3 from t1 where c3 = 333 

Result:

    1, 6, 33, 333

So what I want is to get the corresponding values of the value in the where-statement.

Is there a way to get a solution for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:01:37+00:00Added an answer on May 29, 2026 at 11:01 pm

    (Edit1: simplified, Edit2: wider scope)

    Solutions for any array-length. Also returns all hits, even at multiple positions in one or more arrays at the same time.

    If you know the fixed length of the one-dimensional array, you can hard-code it in generate_series(), for the simplest and fastest solution:

    SELECT id, c1[i], c2[i], c3[i]
    FROM   tbl, generate_series(1, 3) i
    WHERE  c3[i] = 200;
    

    If all arrays in c3 have the same unknown length, you can generalize at very little cost:

    SELECT id, c1[i], c2[i], c3[i]
    FROM   tbl, generate_subscripts((SELECT c3 FROM tbl LIMIT 1), 1) s(i)
    WHERE  c3[i] = 200;
    

    Note that I use the convenient function generate_subscripts() here.
    For a greater subscripts than the upper limit, array elements are just NULL and drop out of the query.


    If the unknown length of the array in c3 actually varies, you can generalize some more, at a higher cost:

    SELECT id, c1[i], c2[i], c3[i]
    FROM   tbl
    FROM   tbl, generate_series(1, (SELECT max(array_upper(c3, 1)) FROM tbl)) s(i)
    WHERE  c3[i] = 200;
    

    Finally, for non-standard array subscripts in addition, the same could be done like this:

    SELECT id, c1[i], c2[i], c3[i]
    FROM   tbl
         , generate_subscripts((SELECT c3 FROM tbl
                                ORDER  BY array_length(c3,1) DESC LIMIT 1), 1) s(i)
    WHERE  c3[i] = 200;
    

    Instant testbed:

    WITH tbl (id, c1, c2, c3) AS (
        VALUES
         (1, '{1, 2, 3}'::int[], '{10, 20, 30}'::int[], '{100, 200, 300}'::int[])
        ,(2, '{4, 5, 6}', '{11, 22, 33}', '{111, 222, 333}')
    -- un-comment to test 333 in all positions of c3 and varying array length
    --  ,(3, '{7, 8, 9, 10}', '{14, 25}', '{333, 333, 333, 333}')
        )
    SELECT id, c1[i], c2[i], c3[i]
    FROM tbl
        ,generate_subscripts((SELECT c3 FROM tbl
                              ORDER  BY array_length(c3,1) DESC LIMIT 1), 1) s(i)
    WHERE  c3[i] = 333;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a problem I ran into recently. I have attributes strings of the form
Here's a coding problem for those that like this kind of thing. Let's see
Here is my problem : I have a post controller with the action create.
Here's an example query: DECLARE @table table (loc varchar(10)) INSERT INTO @table VALUES ('134a'),
Here is an example: I have the generic type called Account. I wish to
Here is a snippet of my xaml: <ComboBox x:Name=cbo1 Width=100 SelectedValue=200> <ComboBoxItem Name=n1>100</ComboBoxItem> <ComboBoxItem
Here's the problem....I have three components...A Page that contains a User Control and a
Here is my problem...I have a page that loads a list of clients and
Here is my problem, I have a multilang database shema. First I want a
Here is the issue I am having: I have a large query that needs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.