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 6319819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:52:07+00:00 2026-05-24T15:52:07+00:00

I have a view defined as: CREATE VIEW View1 AS SELECT Field1, Field2, array_agg(Field3)

  • 0

I have a view defined as:

 CREATE VIEW View1 AS 
 SELECT Field1, Field2, array_agg(Field3) AS AggField 
 FROM Table1 
 GROUP BY Field1, Field2;

What I would like to do is get the intersection of the arrays in AggField with something like:

SELECT intersection(AggField) FROM View1 WHERE Field2 = 'SomeValue';

Is this at all possible, or is there a better way to achieve what I want?

  • 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-24T15:52:08+00:00Added an answer on May 24, 2026 at 3:52 pm

    The closest thing to an array intersection that I can think of is this:

    select array_agg(e)
    from (
        select unnest(a1)
        intersect
        select unnest(a2)
    ) as dt(e)
    

    This assumes that a1 and a2 are single dimension arrays with the same type of elements. You could wrap that up in a function something like this:

    create function array_intersect(a1 int[], a2 int[]) returns int[] as $$
    declare
        ret int[];
    begin
        -- The reason for the kludgy NULL handling comes later.
        if a1 is null then
            return a2;
        elseif a2 is null then
            return a1;
        end if;
        select array_agg(e) into ret
        from (
            select unnest(a1)
            intersect
            select unnest(a2)
        ) as dt(e);
        return ret;
    end;
    $$ language plpgsql;
    

    Then you could do things like this:

    => select array_intersect(ARRAY[2,4,6,8,10], ARRAY[1,2,3,4,5,6,7,8,9,10]);
     array_intersect 
    -----------------
     {6,2,4,10,8}
    (1 row)
    

    Note that this doesn’t guarantee any particular order in the returned array but you can fix that if you care about it. Then you could create your own aggregate function:

    -- Pre-9.1
    create aggregate array_intersect_agg(
        sfunc    = array_intersect,
        basetype = int[],
        stype    = int[],
        initcond = NULL
    );
    
    -- 9.1+ (AFAIK, I don't have 9.1 handy at the moment
    -- see the comments below.
    create aggregate array_intersect_agg(int[]) (
        sfunc = array_intersect,
        stype = int[]
    );
    

    And now we see why array_intersect does funny and somewhat kludgey things with NULLs. We need an initial value for the aggregation that behaves like the universal set and we can use NULL for that (yes, this smells a bit off but I can’t think of anything better off the top of my head).

    Once all this is in place, you can do things like this:

    > select * from stuff;
        a    
    ---------
     {1,2,3}
     {1,2,3}
     {3,4,5}
    (3 rows)
    
    > select array_intersect_agg(a) from stuff;
     array_intersect_agg 
    ---------------------
     {3}
    (1 row)
    

    Not exactly simple or efficient but maybe a reasonable starting point and better than nothing at all.

    Useful references:

    • array_agg
    • create aggregate
    • create function
    • PL/pgSQL
    • unnest
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view that's currently defined as CREATE VIEW riders AS select ridelog
I have an ASP.NET MVC view which contains checkboxes for user-defined categories. <td><% foreach
I have a view that has a list of jobs in it, with data
I have a view using a master page that contains some javascript that needs
I have a view user control that can post form. This control can be
We have a View (call it X) that is the base view called by
I have a view that I want to add some custom drawing to. I
I have a View that can vary significantly, depending on the 'mode' a particular
I have a view which contains a form, the form posts and the data
I have a View class (OrderView.aspx) which shows the details of an order (Account

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.