The output you posted doesn't match with the output of my query using the sample data, but it does what you need.
The articles mention numeric data but it's the same functionality I used on the code I posted. Be sure to understand it well to adapt it to strings as I did using MAX instead of SUM and to be able to do it dynamic if that is what you need.