Richard Huxton <dev@archonet.com> writes: > Search the mailing-list archives for "custom aggregate concat" and > you'll quickly find an example of how to write your own custom aggregate > (like SUM()).
> Warning - I don't think you can guarantee the order of elements in the > aggregated sectors. In recent PG versions you can. For example, select key1, custom_aggregate(detail_field) from (select key1, detail_field from my_table order by key1, key2) ss group by key1 order by key1; The detail_field values will be fed to the aggregate in order by key2 within each key1 group. This is one of the examples that motivated allowing ORDER BY in subselects, even though it's outside the SQL spec. [ experiments... ] This works reliably in 7.4 and up. Before that, the optimizer didn't make the connection between the sort ordering of the inner query and that needed by the outer, so it would repeat the sort step using only key1 and very possibly destroy the key2 ordering. regards, tom lane ---------------------------(end of broadcast)--------------------------- TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]