Help in making sums

Hello, I have the bellow dataset, very simple so far.

No a want to show in a field (whatever the type) on my page, the sum of all values in column “lengte”. I really can’t figure this out how to do so.


Can you please assist?

Many thanks in advance

Hi,
You can create a new collection called “sums” for example and crate a item named “lengteSum”, then you can update its value in a new field everytime you insert an item to the original collection using hooks .
If you need help writing this code we will be happy to help.
Good luck :slight_smile:

Would appreciate if you could help me out in the code

Hi,
The code should look look this, please modify it to match your site.

import wixData from 'wix-data';

export function dataset1_beforeInsert(item, context) {
	console.log(item);
	wixData.query("sums")
		.eq("name", "dataset1")
		.find()
		.then((results) => {
			let firstItem = results.items[0];
			let toUpdate = {"_id": firstItem._id,"sum":firstItem.sum + item.number, "name": firstItem.name}
			wixData.update("sums", toUpdate)
				.then((results) => {
					let item = results; 
				})
				.catch((err) => {
					let errorMsg = err;
				});

		})
		.catch((err) => {
			let errorMsg = err;
		});
	return item;
}

Good luck:)

let CapitalPoolSum = 0;
wixData.aggregate(“YourTableName”)
.sum(“lengte”)
.run()
.then( (results) => {
if (results.items.length > 0) {
CapitalPoolSum = results.items;
console.log(CapitalPoolSum);
} else {
// handle case where no matching items found
}
} )
. catch ( (error) => {
let errorMsg = error.message;
let code = error.code;
} );