It looks like you need an extended version of DISTINCT.
You can do this (in SQL Server) ... via CURSOR.
Load the cursor from your table. Scroll the cursor, map the template, and save what you need by filling in (INSERT) into the working (or temporary) table in each iteration of the cursor.
Choosing from this summary table will present your results. Remove the desktop, or the temp table will be reset at the end of the session.
You can also put this logic in a stored procedure.
Note. Cursors are intense in memory, especially if they are not used properly. But it is very convenient for nontrivial or complex logic.
source share