Not an answer, but there is too much here for a comment.
There are some things in the question that are not clear:
- Can there be more than one set of duplicate for a given set of keys?
- Could there be NO duplicates, and if so what do you want to see?
- Could there be multiple sets of keys with duplicates, and if so how do you want to show this?
Do you really want to show the duplicate row as the column headers? And if so, again, how to handle multiple sets of duplicates? Answered this by looking at revision history. The original did not use the first row as a header.
- What kind of database do you have?
Until you can answer these, this question need to be put on hold, because it's not really answerable.