In this episode, we're tackling the important step of grouping payouts before sending payments or generating our payout spreadsheet. Instead of sending multiple small payments for each referral, we want to combine all payouts for each user into a single amount. For example, if someone has referred three users at $20 each, we don't want to pay them $20 three times—we want to pay them $60 all at once.
We'll walk through the process of updating our database queries so that we group the referral payouts by user and sum up the total amount to pay each one. There's a bit of SQL involved here: we use aggregation functions to sum the values, and we join the users table so we can grab the necessary details—like the user's PayPal email—to include in the final spreadsheet.
To make sure everything works, we test it out by adding payments for multiple users (like "Mabel" and "Alex") and check the spreadsheet-like array output to verify that each user only gets one payout row with the correct total. The result is a much cleaner and more logical way to send mass payments, avoiding unnecessary and confusing multiple small transfers.
By the end of this video, you'll have a solid approach for grouping and summarizing payouts, setting things up perfectly for the next step: generating the spreadsheet and sending out the emails to your users!