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

  • SEARCH
  • Home
  • 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 8753751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:29:07+00:00 2026-06-13T13:29:07+00:00

If I use array_agg to collect names, I get my names separated by commas,

  • 0

If I use array_agg to collect names, I get my names separated by commas, but in case there is a null value, that null is also taken as a name in the aggregate. For example :

SELECT g.id,
       array_agg(CASE WHEN g.canonical = 'Y' THEN g.users ELSE NULL END) canonical_users,
       array_agg(CASE WHEN g.canonical = 'N' THEN g.users ELSE NULL END) non_canonical_users
FROM groups g
GROUP BY g.id;

it returns ,Larry,Phil instead of just Larry,Phil (in my 9.1.2, it shows NULL,Larry,Phil).

Instead, if I use string_agg(), it shows me only the names (without empty commas or nulls).

The problem is that I have Postgres 8.4 installed on the server, and string_agg() doesn’t work there. Is there any way to make array_agg work similar to string_agg() ?

  • 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-06-13T13:29:08+00:00Added an answer on June 13, 2026 at 1:29 pm
    select
        id,
        (select array_agg(a) from unnest(canonical_users) a where a is not null) canonical_users,
        (select array_agg(a) from unnest(non_canonical_users) a where a is not null) non_canonical_users
    from (
        SELECT g.id,
               array_agg(CASE WHEN g.canonical = 'Y' THEN g.users ELSE NULL END) canonical_users,
               array_agg(CASE WHEN g.canonical = 'N' THEN g.users ELSE NULL END) non_canonical_users
        FROM groups g
        GROUP BY g.id
    ) s
    

    Or, simpler and may be cheaper, using array_to_string which eliminates nulls:

    SELECT
        g.id,
        array_to_string(
            array_agg(CASE WHEN g.canonical = 'Y' THEN g.users ELSE NULL END)
            , ','
        ) canonical_users,
        array_to_string(
            array_agg(CASE WHEN g.canonical = 'N' THEN g.users ELSE NULL END)
            , ','
        ) non_canonical_users
    FROM groups g
    GROUP BY g.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In case of one-dim array I can use array_unique to get unique entries. But
When I use array_merge() with associative arrays I get what I want, but when
While setting GET parameters for Zend_Http_Client, you can use array with name-value pairs or
Is there a class that represents an unordered array in Ruby? I cannot use
I'm working on a scheme that will heavily use array fields in mongo documents,
I want to take an array and use that array's values to populate an
In what cases I should use Array(Buffer) and List(Buffer). Only one difference that I
I use array_multisort() function to sort an array by a field value. what I
Possible Duplicate: Is it okay to use array[key] in PHP? Is there any significant
I'm trying to use a string array as a holder of different variable names

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.