Question:
Hello, I would like to doo an aggregation (sum) on 2 fields, and group them by another field
The aggregation works with only one field but not with 2 ones. (“TotalTechNormal” and “TotalTechNormal”)
here the code :
let Recaptech =await wixData.aggregate(“Tests”).group(“nomTechnicien”, “date”,).sum(“TotalTechSimple”+“TotalTechNormal”,“TOTAL”).run();
How to do this please.
Product:
WIX EDITOR
What are you trying to achieve:
I want to group and aggregate results of my employees and group by date / name that works (ok), and sum results In 2 categories (Simple/Normal : doesn’t work)
Thanks a lot
In JavaScript, when you “add” (+
) two strings together, they concatenate - You’re running the function
.sum('installationSimpleTotalTechSimple', 'TOTAL')
You can chain multiple .sum()
functions
.sum('installationSimple', 'totalInstallationSimple').sum(...)
For a total of both fields, you could just add the results together. As for a function that does this itself, I do notk now
Thanks a lot for your help.
of course the solution would be to just add the results together, but yhe result is beint saved in a table or repeater.
here the code :
// “***************”//
async function UpdateTableau(){
let Recaptech =await wixData.aggregate(“Tests”).group(“nomTechnicien”, “date”,).sum(“installationSimple”,“TotalTechSimple”).sum(“installationNormale”,“TotalTechNormal”).sum(‘TotalTechSimple’+‘TotalTechNormal’,‘TotalCumule’).run();
$w(‘#TableauTech’).rows=Recaptech.items
console.log(Recaptech.items)
}
$w(‘#button32’).onClick(()=>{UpdateTableau()})
});
Why aggregate then? Just let the repeater do the calculation on the client side
$w('#repeater').onItemReady(($item, itemData) => {
$item('#sum').text = itemData.simple + itemData.normal
})
This will save the server unnecessary processing, while not really affecting the client in any meaningful way
In fact, i extract data from a database collection called “TESTS” and i want to display the details (NORMAL and SIMPLE) by tech and date (Group technicien and date) and i have a column with the total (to add simple+normal) in a table called “Recaptech”
here my code, the cms, and the table where data are put in (photo)
My code calculates :
- the simple by tech and by date (group) and display in the column Total Simple
- the normal by tech and by date (group)and display in the column Total Normal
The pb : i dont know how to get the total of these one and piut them in the column " Total cumulé"
// ****** CODE ******* //
let SessionMailClt
SessionMailClt=session.getItem(‘MailClt’);
console.log('Mail client à afficher: ',SessionMailClt)
$w('#dataset1').setFilter(wixData.filter()
.eq("mailClt", SessionMailClt))
.then(result)
const filtre = wixData.filter().eq(“mailClt”, SessionMailClt);
async function UpdateTableau(){
let Recaptech =await wixData.aggregate(“Tests”).group(“nomTechnicien”, “date”,).sum(“installationSimple”,“TotalTechSimple”).sum(“installationNormale”,“TotalTechNormal”).sum(‘TotalTechSimpleTotalTechNormal’,‘TotalCumule’).run();
$w(‘#TableauTech’).rows=Recaptech.items
console.log(Recaptech.items)
//$w(‘#TableauTech’). = itemData.simple + itemData.normal
}
$w(‘#button32’).onClick(()=>{CalcultTotalTechnicien()})
$w(‘#button32’).onClick(()=>{UpdateTableau()})
});