Is there a way to assemble one column when I have many other columns in the query?
I tried this answer , which works, but my request became much more verbose.
My current request looks like this:
SELECT t1.foo1, t1.foo2, t2.foo3, t2.foo4, string_agg(t3.aggregated_field, ', ') FROM tbl1 t1 LEFT JOIN tbl2 t2 ON t1.id = t2.fkeyid LEFT JOIN tbl3 t3 ON t2.id = t3.fkeyid GROUP BY t1.foo1, t1.foo2, t2.foo3, t2.foo4, t2.foo5, t2.foo6 ORDER BY t2.foo5, t2.foo6
There are many more fields and LEFT JOIN s in the request, the important part is that all these fields have relations from 1 to 1 or from 1 to 0, with the exception of one field, which from 1 to n, which I want to fill, presented t3.aggregated_field in the pseudo query above.
Since I use an aggregate function, all fields listed in SELECT and ORDER BY must be either aggregated or part of the GROUP BY . This makes my request more verbose than it is.
That is, if foo1 is the primary key, when this field is repeated, all the others except aggregated_field are also equal. I want these duplicate rows to be the result of a single row with an aggregated field value. (basically a select distinct with aggregate column)
Is there a better way to do this (without having to put all the other fields in GROUP BY ) or just iterate over the result set in my source code by querying for each row that gets this from 1 to n relationships?
The server runs PostgreSQL 9.1.9, namely:
PostgreSQL 9.1.9 on x86_64-unknown-linux-gnu, compiled gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54), 64-bit