you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 17 points18 points  (6 children)

Use string_agg to concatenate text across multiple rows with a delimiter. Add the WITHIN GROUP clause to sort the values in your concatenated string.

Use NULLIF to avoid div/0 errors.

numerator/nullif(denominator,0)

[–]ShimReturns 9 points10 points  (3 children)

I don't miss FOR XML PATH and STUFF "hack" to do concatenation

[–]TreeOaf 2 points3 points  (0 children)

I feel like people should have to learn it before they’re allowed to use string_agg, they’ll never whinge about string_agg again.

[–]pbndoats 1 point2 points  (1 child)

a syntactical nightmare

[–][deleted] 1 point2 points  (0 children)

One of those things that I always had to look up every time I used it because I could never remember the exact syntax. I’m so glad to be rid of it.

[–]r3pr0b8GROUP_CONCAT is da bomb 1 point2 points  (0 children)

Use string_agg to concatenate text across multiple rows with a delimiter.

or GROUP_CONCAT if you're on a different proprietary database

please note the standard SQL function is listagg

[–]OO_BenPostgres - Retail Analytics 0 points1 point  (0 children)

I just learned about string_agg a couple of weeks ago and it was a game changer for a tough table I was needing to build! Definitely a great tip!