I have read your questions and I think I am on the right track. If you can program the routines, but can't seem to organize the data, then I can help. It's how you store the values in variables, then access them to Mae the output appear in the array you are talking about. I understand databases really well...
Shit if you saw the tables I created in this telco I used work for in the 90s .. Imagine a telephone company with only certain info on each call... Cuts no, time call was made, origin of call, destination of the call, and duration of the call. Then there were all these cross reference tables to determine what discount we gave in what regions in the world, and at what time of say as discounts were given in non peak hours... Lol and I have to create a program that actually made sense of all that and produced a bill for each customer.
This is much the same you need here, without all the cross tables, you have all the data you need in each record. All you need to do is store the values in variables, add to them them output them in the appropriate format.
Is this what you are stuck with?