Group by column and add other columns accordingly

For the learnings have a look here: