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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:24:27+00:00 2026-06-11T11:24:27+00:00

I am using PostgreSQL 9.1 and need help with concatenating multiple rows in one.

  • 0

I am using PostgreSQL 9.1 and need help with concatenating multiple rows in one. I need to do that in 2 tables. When I use two times array_agg() functions I get duplicated values in result.

Tables:

CREATE TABLE rnp (id int, grp_id int, cabinets varchar(15) );

INSERT INTO rnp VALUES
 (1,'11','cabs1')
,(2,'11','cabs2')
,(3,'11','cabs3')
,(4,'11','cabs4')
,(5,'22','c1')
,(6,'22','c2');

CREATE TABLE ips (id int, grp_id int, address varchar(15));

INSERT INTO ips VALUES
 (1,'11','NY')
,(2,'11','CA')
,(3,'22','DC')
,(4,'22','LA');

SQL:

SELECT DISTINCT

  rnp.grp_id,
  array_to_string(array_agg(rnp.cabinets)OVER (PARTITION BY rnp.grp_id), ',') AS cabinets,
  array_to_string(array_agg(ips.address) OVER (PARTITION BY ips.grp_id), ',') AS addresses


FROM rnp JOIN ips ON rnp.grp_id=ips.grp_id

Result:

GRP_ID  CABINETS                                             ADDRESSES
11  cabs1,cabs1,cabs2,cabs2,cabs3,cabs3,cabs4,cabs4     NY,CA,NY,CA,NY,CA,NY,CA
22  c1,c1,c2,c2                                             DC,LA,DC,LA

And what I need is:

 GRP_ID     CABINETS                 ADDRESSES
    11  cabs1,cabs2,cabs3,cabs4       NY,CA,
    22  c1,c2                         DC,LA

This example in SQLFiddle: http://sqlfiddle.com/#!1/4815e/19

There is no problem if use one table – SQLFiddle: http://sqlfiddle.com/#!1/4815e/20

What am I missing? Is it possible to do this, because of JOIN?

  • 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-11T11:24:28+00:00Added an answer on June 11, 2026 at 11:24 am

    Instead of using window functions and partitioning, use a query-level GROUP BY and aggregate with a DISTINCT clause:

    SELECT         
      rnp.grp_id,
      array_to_string(array_agg(distinct rnp.cabinets),',') AS cabinets,
      array_to_string(array_agg(distinct ips.address),',')  AS addresses
    FROM rnp JOIN ips ON rnp.grp_id=ips.grp_id GROUP BY rnp.grp_id, ips.grp_id;
    

    Result:

     grp_id |        cabinets         | addresses 
    --------+-------------------------+-----------
         11 | cabs1,cabs2,cabs3,cabs4 | CA,NY
         22 | c1,c2                   | DC,LA
    (2 rows)
    

    The key here is that instead of using window functions and patitioning, you use a query-level GROUP BY and aggregate with a DISTINCT clause.

    This’d work with the window function approach too, except that PostgreSQL (9.1 at least) doesn’t support DISTINCT in window functions:

    regress=# SELECT DISTINCT
      rnp.grp_id,
      array_to_string(array_agg(distinct rnp.cabinets)OVER (PARTITION BY rnp.grp_id), ',') AS cabinets,                    
      array_to_string(array_agg(distinct ips.address) OVER (PARTITION BY ips.grp_id), ',') AS addresses
    FROM rnp JOIN ips ON rnp.grp_id=ips.grp_id;
    ERROR:  DISTINCT is not implemented for window functions
    LINE 3:   array_to_string(array_agg(distinct rnp.cabinets)OVER (PART...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using PostgreSQL with pgadmin. I need to replace multiple characters in all fields
I have developed an application using postgresql and it works well. Now I need
Using PostgreSQL triggers, is it possible to record the changes that have happened to
I'm using PostgreSQL 9.1.3 and the following functions: CREATE OR REPLACE FUNCTION cad(INOUT args
Since I'm using Postgresql and can't use LINQ to SQL, I wrote my own
postgresql python I need some help fixing some issues with a postgresql database I
Using PostgreSQL (8.x), I need to skip a block of sequence numbers automatically ,
Assume that you have this code in C#/.NET (using PostgreSQL via ODBC): using System.Data.Odbc;
I am using Postgresql,hibernate and Java and I need to store a password. Can
I am using postgreSQL database. I need to add Numeric and Boolean parametres with

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.