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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:16:37+00:00 2026-05-25T15:16:37+00:00

I have two tables, table1 has a entry_ID , entry_date and other entry information.

  • 0

I have two tables, table1 has a entry_ID, entry_date and other entry information. table2 has entry_ID and entry_subject. Each entry_ID can have arbitrarily many entry_subjects.

I want a query that will return an entry_ID, entry_date, and a list of the subjects corresponding to that entry separated by commas.

The first step in this seems to be just getting a query that returns an entry_ID and a comma separated list of subjects from table2. Once I have that the join should be easy.
I adapted the recursive CTE method from this site: to fit my case:

WITH RECURSIVE CTE (entry_ID, subjectlist, subject, length)
    AS ( SELECT entry_ID, cast( '' as varchar(8000))
                        , cast( '' as varchar(8000)), 0
         FROM table2 
         GROUP BY entry_ID
         UNION ALL 
         SELECT t2.entry_ID, 
             cast(subjectlist || CASE length = 0 THEN '' ELSE ', ' END
                              || entry_subject AS varchar(8000) ),
             cast (t2.entry_subject as varchar(8000)),
             length +1
         FROM CTE c 
         INNER JOIN table2 t2 
             on c.entry_ID=t2.entry_ID where t2.entry_subject > c.subject)
SELECT entry_ID, subjectlist FROM (
    SELECT entry_ID, subjectlist, RANK() OVER (
        PARTITION BY entry_ID order by length DESC)
    FROM CTE) D (entry_ID, subjectlist, rank) where rank = 1;

And it works, I get the response I expect. To achieve my final goal the query I use is this:

SELECT t1.* t2.subjectlist FROM table1 
    JOIN (ABOVE QUERY) AS t2 on t1.entry_ID=t2.entry_ID; 

This seems very unwieldy. Is this really the best way to do 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-25T15:16:38+00:00Added an answer on May 25, 2026 at 3:16 pm

    If I understand correctly, then there should be a much simpler solution.

    Test setup

    According to your description – you could have done that for us:

    CREATE TABLE table1 (
       entry_id int4 PRIMARY KEY
     , entry_date date
    );
    
    CREATE TABLE table2 (
       entry_id int4 REFERENCES table1 (entry_id)
     , entry_subject text
     , PRIMARY KEY (entry_id, entry_subject)
    );
    
    INSERT INTO table1 VALUES (1, '2011-09-01'), (2, '2011-09-02'),(3, '2011-09-03');
    INSERT INTO table2 VALUES (1, 'foo1'), (2, 'foo2'), (2, 'bar2')
                            , (3, 'foo3'), (3, 'baz3'), (3, 'bar3');  
    

    Answer

    string_agg() requires Postgres 9.0+

    SELECT t1.entry_id, t1.entry_date
         , string_agg(t2.entry_subject, ', ') AS entry_subjects
    FROM   table1 t1
    JOIN   table2 t2 USING (entry_id)
    GROUP  BY 1,2
    ORDER  BY 1;
    
     entry_id | entry_date | entry_subjects
    ----------+------------+------------------
            1 | 2011-09-01 | foo1
            2 | 2011-09-02 | bar2, foo2
            3 | 2011-09-03 | baz3, bar3, foo3
    

    Or, if you want the entry_subjects sorted:

    SELECT DISTINCT ON (1)
           t1.entry_id
         , t1.entry_date
         , string_agg(t2.entry_subject, ', ') OVER (
              PARTITION BY t1.entry_id ORDER BY t2.entry_subject
              RANGE BETWEEN UNBOUNDED PRECEDING
                        AND UNBOUNDED FOLLOWING) AS entry_subjects
      FROM table1 t1
      JOIN table2 t2 USING (entry_id)
      ORDER BY 1;
    
     entry_id | entry_date | entry_subjects
    ----------+------------+------------------
            1 | 2011-09-01 | foo1
            2 | 2011-09-02 | bar2, foo2
            3 | 2011-09-03 | bar3, baz3, foo3
    

    You could do the same with a subselect on table2 to first ORDER BY entry_subject.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: table1, table2. Table1 has 10 columns, table2 has 2 columns.
I have two tables (TABLE1, TABLE2 - unique i know) that has a 1-to-many
I have three tables, which are each 1:n. An entry in table1 has n
I have two tables one has a three column composite key. The other needs
i have two tables products and reviews each product has several reviews linked by
I have two tables. One has order information with an order id, and the
I have two tables, related by a common key. So TableA has key AID
I have a database framework where I have two tables. The first table has
I have two tables: object that has object_id column and avalues that have object_id
I have two tables: a 'userlist' that has a 'grade' and 'userID' columns, and

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.