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?
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:
Answer
string_agg()requires Postgres 9.0+Or, if you want the entry_subjects sorted:
You could do the same with a subselect on
table2to firstORDER BY entry_subject.