# Aggregate from one collection, insert to another - with multiple groupings

**URL:** <https://forum.wixstudio.com/t/aggregate-from-one-collection-insert-to-another-with-multiple-groupings/77598>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [April 10, 2026, 10:07pm UTC](https://forum.wixstudio.com/t/aggregate-from-one-collection-insert-to-another-with-multiple-groupings/77598 "2026-04-10T22:07:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amy\_Buckingham\_Zieli](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amy_buckingham_zieli/32/101552_2.png) [@Amy\_Buckingham\_Zieli](https://forum.wixstudio.com/u/Amy_Buckingham_Zieli)\
**Post date:** [April 10, 2026, 10:07pm UTC](https://forum.wixstudio.com/t/aggregate-from-one-collection-insert-to-another-with-multiple-groupings/77598/1 "2026-04-10T22:07:33Z")

</div>

**I’m having trouble with**  
Aggregating points based on 2 different groupings, then applying those totals to another collection

**Working in**  
Dev Mode

**What I’m trying to do**  
A horse is assigned a division to collect points in. They may _also_ have _another_ division to accumulate points in. I would like to collect the points gathered in multiple classes (database 2026 High Points) and update the horse’s records for the “First Division” and “Second Division” (database Horse Reference).

**What I’ve tried so far**  
I have done all the Googles. I’m not sure if it is better to put this through an automation (not sure how to make that work properly) or if I should have it on an “after Update” for 2026 High Points. I already have something in the 2026 High Points after update and I’m not sure how to make sure that doesn’t get messed up also. I’ve tried this in Sandbox _and_ live. I’ve tried with a button trigger, and I’ve tried as an “after Update”.

**Screenshots of Database Set-Up First, Then Code (If I Use A Button)**

 ![Screenshot 2026-04-10 175349](https://us1.discourse-cdn.com/wix/original/3X/b/3/b300f9700749dafbf77e213eeb7b54e7ac4fa8cf.png)

Database 2026 High Points: “\_id”, “recordId”, “horseId”, “division”, “showName”, “className”, “championshipStake”, “placing”, “pointsEarned”

![Screenshot 2026-04-10 175431](https://us1.discourse-cdn.com/wix/optimized/3X/f/7/f7201abd83c9c9b02899cc0ad1f6f2e5f5165da3_2_690x38.png)

Database Horse Reference: (\_id, not shown), “recordId”, “horseName”, “firstDivision”, “firstDivisionPoints”, “secondDivision”, “secondDivisionPoints”

```javascript
export async function syncAggregatedData() {
	// 1. Get aggregate results (e.g., sum of points per recordID and division)
	const sumAgg = await wixData.aggregate("2026highpoints")
        .group("recordId", "division")
        .sum("pointsEarned")
        .run();
    const aggresults = sumAgg.items; // Array of {recordId, division, pointsEarnedSum}

	// 2. Query target collection for matching items
    const horserefs = await wixData.query("HorseReference")
        .include("firstDivision", "secondDivision")
        .find();
	
	// 3. Match and prepare items for update
	const itemsToSave = horserefs.items.map(horseinfo => {
		// Find matching aggregate for the horse
		const matchdivone = aggresults.find(agg =>
			agg.recordId === horseinfo.recordId &&
			agg.division === horseinfo.firstDivision);
		const matchdivtwo = aggresults.find(agg =>
			agg.recordId === horseinfo.recordId &&
			agg.division === horseinfo.secondDivision);
		
		if (matchdivone || matchdivtwo) {
			horseinfo.firstDivisionPoints = matchdivone.pointsEarnedSum;
			horseinfo.secondDivisionPoints = matchdivtwo.pointsEarnedSum;
			return horseinfo;
		}
		return null;
	}).filter(item => item !== null);

	// 4. Bulk save
	if (itemsToSave.length > 0) {
		return wixData.bulkSave("HorseReference", itemsToSave);
	}
}

```

This is something I have less experience with – and again, I’m doing this in my spare time for a not-for-profit. Can’t be paying someone to do it for me, unfortunately. (If needed, the database set-up being referenced for “divisions” will be in the comments.)

---

<div class="post-metadata">

**Author:** ![Amy\_Buckingham\_Zieli](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amy_buckingham_zieli/32/101552_2.png) [@Amy\_Buckingham\_Zieli](https://forum.wixstudio.com/u/Amy_Buckingham_Zieli)\
**Post date:** [April 10, 2026, 10:08pm UTC](https://forum.wixstudio.com/t/aggregate-from-one-collection-insert-to-another-with-multiple-groupings/77598/2 "2026-04-10T22:08:09Z")

</div>

![Screenshot 2026-04-10 175507](https://us1.discourse-cdn.com/wix/optimized/3X/2/e/2efca5d27aa13ed4a339d025a1c385a823ab652d_2_690x42.png)

Database High Point Divisions: “\_id”, “title”, “divisionName”, “divisionCategory”

---

<div class="post-metadata">

**Author:** ![Lauren\_Cintas](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/lauren_cintas/32/118442_2.png) [@Lauren\_Cintas](https://forum.wixstudio.com/u/Lauren_Cintas)\
**Post date:** [April 12, 2026, 12:23am UTC](https://forum.wixstudio.com/t/aggregate-from-one-collection-insert-to-another-with-multiple-groupings/77598/3 "2026-04-12T00:23:17Z")

</div>

On my actual account not the account owner’s…. anyone have any ideas? I’m sure it is something silly that isn’t working.
