grouping and summing

Hi,
I’ve been stuck on this for a week and can’t figure it out. I have a collection that stores baseball game results (SCORES collection) - game date, winning team, winning team score, losing team, and losing team score. I am trying to create a standings list using that collection data. The standings would be Team Name, Wins, Losses, Runs For, Runs Against.
So I was able to piece together how to group by the winning team and get a count on that. That solves get the count of wins for each winning team in the SCORE collection.

wixData.aggregate("Scores") 
.group("winningTeam") 
.count() 
.sum("winningTeamScore","WinningTeamTotalRuns")
.run() 
.then( (results) => { 
console.log(results.items);

I can’t figure out how to get the rest though. I would need to group on losing team and get that count. Then I have to get the total runs scored by a team (which is winningTeamTotalRuns from above and then the same on the losing side). Do I have 2 different aggregate sections with one as above and one for losing team? And then group/add them together someway? And I figure all this should get inserted back into a STANDINGS collection which could make it easier to be displayed as a list on a page.
Thanks for any help.

1 Like