Can you supply the DDL and DML as well as sample data and expected results as this will help us understand the data and what results you want.
Is there a reason why you need to include RecordId in the output?
_________________________________________________________________________
SSC Guide to Posting and Best Practices