Skip to content
Advertisement

grouping rows while being able to show columns from each row in the group

I’m sure the title is not clear enough, couldn’t find a better way to explain what I need.

I’m using PostGreSQL 11.4

i’ll minify the problem i’m having to show the actual issue.

I have a query that returns the following:

what I would like to do is to group by id, time and to add two new columns: name_direction1 and name_direction2

so the result table would be:

as far as I know with group by I can’t really reach each one of the rows. in my case I need to combine two rows when both directions are presented.

any ideas how to achieve this?

thank you!

Advertisement

Answer

With conditional aggregation:

See the demo.
Results:

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