Skip to content
Advertisement

postgres – aggregate items in text array

I have an array of text items, each text item in the array is a delimited string, here is a sample:

sample output:

I’m interested in extracting the price, which is represented as the second item of each array entry (when delimited by ~), so that the output would be:

when using split_part, that return only one result of a specific array index, and I do not want to hard-code all indexes, since it varies.

suggestions are appreciated.

Advertisement

Answer

Here is one approach:

Within the subquery, we split the string to rows, using separator |; then, we aggregate the second element of each row back into an array.

You can be explicit that you want to preserve the ordering of the elements with with ordinality:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement