Smart functions in Wix Data Query

Let’s say I want to sum all orders in my orders Data Collection. Today I need to do this with scripts.

I would like to perform the following in Wix Code.

import wixData from 'wix-data';   
wixData.query("Customer") 
.sum("orderAmount") 
.find() 
.then( (results) => {      
    console.log(results.items.sum); 
} );

The above query will sum all my orders using the field orderAmount and it will give me the result back in the items.sum result. This would be great.

The next one is about getting the order with highest orderAmount fast.

import wixData from 'wix-data';    
wixData.query("Customer") 
.max("orderAmount") 
.find() 
.then( (results) => {           
    console.log(results.items.max); 
} ); 

Small things but they would be appreciated.

1 Like

This will be very useful, but I think the ideal would be to have that functionality when connecting/binding a text box to the dataset numeric field - right on the page.
So when you you select [Text connects to] and you pick a number field, further options should be made available with math functions e.g. sum, min, max , avg etc
This will be useful when creating a report like order totals etc.

to expand on this idea.

  1. The same could be done for other types of fields like date and time
  2. Also referring to Andreas’s other idea of adding formulas in the collection field during collection design. How about having that capability on the page itself. e.g. bind the text box to the dataset and use a formula/function to calculate a value (in stead of doing that with a script) just by entering e.g. =sum(“fieldname”) or =“fieldname”*(“fieldname”+“fieldname”)