group by - how to retrieve the column values which are aggregated(like count) using groupby column -


i want display contents of aggregated columns part of group sql statement. example:

select shippers.shippername,count(orders.orderid) numberoforders orders orders.shipperid=shippers.shipperid group shippername 

in above example output gives me count 1 of result, whereas need aggregated orders.orderid actual values even. if 1 result count shows me 2. need know 2 values have been grouped. result should column in same table.

try group_concat

 select shippers.shippername,count(orders.orderid) numberoforders , array_agg(orders.orderid) which_are_those orders  orders.shipperid=shippers.shipperid  group shippername 

array_agg returns array, can cast text , edit needed (see clarifications, below).

prior version 8.4, have define prior use:

create aggregate array_agg (anyelement) ( sfunc = array_append, stype = anyarray, initcond = '{}' ); 

or this:

select shippers.shippername,count(orders.orderid) numberoforders , string_agg(orders.orderid, ',') which_are_those orders  orders.shipperid=shippers.shipperid  group shippername 

Comments