Good afternoon. I have a CSV file and reading it row by row (skipping the header row). The CSV contents look like this:
AccountNumber,CallStartDateTime,CallType,Duration,PerMinRate,Amount,SubAccountID
5004017,9/24/2023 15:29,WVO,6,0.003,0.0003,18499
5004017,9/24/2023 21:37,WVO,54,0.003,0.0027,18499
5004017,9/24/2023 21:38,INTERSTATE,60,0.00211,0.00211,18499
5004017,9/25/2023 11:30,WVO,6,0.003,0.0003,18499
5004017,9/25/2023 13:34,WVO,6,0.003,0.0003,18499
5004017,9/25/2023 13:43,INTERSTATE,54,0.001,0.0009,19203
5004017,9/25/2023 13:52,WVO,6,0.003,0.0003,18499
5004017,9/25/2023 13:43,WVO,54,0.003,0.0027,19203
5004017,9/25/2023 14:00,WVO,48,0.003,0.0024,19203
In summary, I want to calculate the sum total of "Amount" for each SubAccountID and CallType.
In this example, row 2 – call type is WVO for subaccount 18499. The amount is 0.0003. Currently, a variable for this subaccount and call type doesn’t exist, so it creates the variable and the current amount sum is 0.0003.
Row 3 – call type is WVO for subaccount 18499. the amount is 0.0027. I then take the current variable for this call type and amount and add 0.0027 to the current 0.0003. The variable is now 0.0030.
Row 4 – call type is INTERSTATE for subaccount 18499. The amount is 0.00211. Currently, a variable for this subaccount and call type doesn’t exist, so it creates the variable and the current amount sum is 0.00211.
Does anyone have some pointers on how to solve this? The file read-by-row function is very straightforward, but I’m struggling on the logic to dynamically create and sum these variables. The tricky part is that I won’t necessarily know the SubAccountIDs
I haven’t been able to wrap my head around how to structure the code to perform the required functions
2
Answers
Thanks Rob! That's exactly what I was looking for. I couldn't wrap my head around using the nested arrays but your example got me exactly where I needed to be!
You can iterate through the file and build up an array of sums – for example by call type and sub account:
Gives this output: