Coding Custom Report
I suggest you install the extended reports extension and see if one of those reports does what you want. The extension provides many reports and is well maintained.

If it doesn't provide what you want, perhaps reviewing the code of extended reports that do group by financial account code will help you find the answer to your problem. (Sorry I am not a coder, so can't provide any help with that.)

Also are you sure you need the information on the individual transactions and the account code totals in the same report. My understanding is that often it easier to do one or the other on a given report, rather than both. That is why there are summary and detail pairs of reports. (eg for Donor Details and Event Income).