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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:01:08+00:00 2026-05-14T04:01:08+00:00

I have a table and I’d like to pull one row per id with

  • 0

I have a table and I’d like to pull one row per id with field values concatenated.

In my table, for example, I have this:

TM67 | 4  | 32556
TM67 | 9  | 98200
TM67 | 72 | 22300
TM99 | 2  | 23009
TM99 | 3  | 11200

And I’d like to output:

TM67 | 4,9,72 | 32556,98200,22300
TM99 | 2,3    | 23009,11200

In MySQL I was able to use the aggregate function GROUP_CONCAT, but that doesn’t seem to work here… Is there an equivalent for PostgreSQL, or another way to accomplish 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-14T04:01:08+00:00Added an answer on May 14, 2026 at 4:01 am

    This is probably a good starting point (version 8.4+ only):

    SELECT id_field, array_agg(value_field1), array_agg(value_field2)
    FROM data_table
    GROUP BY id_field
    

    array_agg returns an array, but you can CAST that to text and edit as needed (see clarifications, below).

    Prior to version 8.4, you have to define it yourself prior to use:

    CREATE AGGREGATE array_agg (anyelement)
    (
        sfunc = array_append,
        stype = anyarray,
        initcond = '{}'
    );
    

    (paraphrased from the PostgreSQL documentation)

    Clarifications:

    • The result of casting an array to text is that the resulting string starts and ends with curly braces. Those braces need to be removed by some method, if they are not desired.
    • Casting ANYARRAY to TEXT best simulates CSV output as elements that contain embedded commas are double-quoted in the output in standard CSV style. Neither array_to_string() or string_agg() (the “group_concat” function added in 9.1) quote strings with embedded commas, resulting in an incorrect number of elements in the resulting list.
    • The new 9.1 string_agg() function does NOT cast the inner results to TEXT first. So “string_agg(value_field)” would generate an error if value_field is an integer. “string_agg(value_field::text)” would be required. The array_agg() method requires only one cast after the aggregation (rather than a cast per value).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table called Direccion other called Cliente each one defined like this public
I have table field defined as like this approved_at DATETIME YEAR TO SECOND NOT
I have table in an MS Access database that looks like this: ID Symbol
I have table Owner in my database where one of the field (System) saved
I have table (call it my_table ) that can be simplified like this: NAME,
I have table like this: +------+-------+ | user | data | +------+-------+ | 1
I have table called location with a field area. This field can contain various
I have table Table1 . With fields f1, f2 The table values are like
I have table structure like this: ID cond 1 5 1 2 1 6
I have table data like this id id1 name 1 1 test1 1 1

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.