It appears our first shot at this was based on a false assumption about the database structure and data. After another look at the tables, we diagnosed the problem and ran another query against the db, retrieved the results, and formatted them as before. These results appear to be right.