I have a question based on [this fiddle](https://dbfiddle.uk?rdbms=postgres_9.5&fiddle=acb59b827302500cb9cf10e512fa997a), but my question is different and the corresponding sample data is as follows (updated the sample data to add `id` column for ordering within each group):
For each group `grp`, I would like to merge/combine the values of `low,` `high` based on `avg` using `axis_1` and `axis_2` as controls.
For each `grp`, if the difference between `avg` values (the preceding and the succeeding ones) is zero (same value) then merge all such rows (with `low` of the first row to the `high` of the last merged row). The merging/combining is not allowed across the `axes` values (`axis_1` and `axis_2`), that is, rows should not be merged crossing over the axes values. The `low` and `high` of the merged rows need to be in order as expected output.
The expected output (`grp` and `text/string array`) is as follows. The `merged_row array `is in the format `low,high,avg...` (for all possible "sub-groups" in a `grp`).
Can someone help me to achieve the expected output?
You need several steps to do this:
1. split groups and islands (modified to break on axis match)
2. get and combine the first and last rows in each subgroup/island
3. aggregate the results for the islands into a single array per group
This is the final query — visit the fiddle to see each step building up: