If I have a query like this:
SELECT
u.client_code,
max(d.created_at) as last_document_created_at,
u.brand_id,
t.name as template_name,
count(d)
FROM users u
INNER JOIN documents d ON u.id = d.user_id
INNER JOIN templates t ON t.id = d.template_id
GROUP BY 1, 3, 4
Which returns information like this:
client_code last_document_created_at brand_id template_name count
---------------------------------------------------------------------
client1 2017-12-06 10:03:47 +1100 39 newsletter_r 1
client1 2017-12-05 15:23:24 +1100 39 Other media 5
client2 2017-12-21 17:07:11 +1100 39 newsletter_r 4
client3 2018-01-11 12:10:43 +1100 39 newsletter_r 2
client3 2017-12-06 11:45:21 +1100 39 Other media 1
What are my options to concatenate the template_name and count fields so that each user (represented in u.client_code) is on one line? I know I can call string_agg the column like so:
...
string_agg(distinct t.name, ', ') as template_name,
...
But that of course ruins the respective counts:
newsletter_r, Other media 6
Update
I could do this:
string_agg(concat_ws(': ', t.name::text, count(d)::text), ', ') as template_count
But that gives me an error:
aggregate function calls cannot be nested LINE 5: string_agg(concat_ws(': ', t.name::text, count(d)::text)... ^ : SELECT u.client_code,