Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • 1
    And in 9.0 you will have listagg() Commented Apr 1, 2010 at 16:55
  • 8
    To get CSV the query should be: SELECT id_field, array_to_string(array_agg(value_field1), ','), array_to_string(array_agg(value_field2),', ') FROM data_table GROUP BY id_field Commented Feb 16, 2012 at 16:08
  • 2
    You can't use array_to_string in all cases here. If your value_field contains an embedded comma, the resulting CSV is incorrect. Using array_agg() and casting to TEXT properly quotes strings with embedded commas. The only caveat is that it also includes the starting and ending curly braces, hence my statement "and edit as needed". I will edit to clarify that point. Commented Feb 21, 2012 at 20:00
  • FYI: here's a link to docs on array_agg in 8.4 Commented Apr 22, 2014 at 21:35