QueryReduce
This function reduces the query to a single value.
Method Signature
QueryReduce(query=[query], callback=[function:BiFunction], initialValue=[any])
Arguments
Argument
Type
Required
Description
Default
query
query
true
The query to iterate over
callback
function:BiFunction
true
The function to invoke for each item. The function will be passed 4 arguments: the accumulator, the current item, the current index, and the query. You can alternatively pass a Java Predicate which will only receive the first 2 args.
initialValue
any
true
The initial value to use for the reduction
Examples
Reduce column to total
Sum one query column
fruits = queryNew( "fruit,amount", "varchar,integer", [
{
"fruit" : "apples",
"amount" : 15
},
{
"fruit" : "pineapples",
"amount" : 3
},
{
"fruit" : "strawberries",
"amount" : 32
}
] );
total_fruits = queryReduce( fruits, ( Any a, Any b ) => {
return a + b.AMOUNT;
}, 0 );
writeOutput( total_fruits );
Result: 50
Additional Examples
people = QueryNew( "name,dob,age", "varchar,date,int", [
[
"Susi",
CreateDate( 1970, 1, 1 ),
0
],
[
"Urs",
CreateDate( 1995, 1, 1 ),
0
],
[
"Fred",
CreateDate( 1960, 1, 1 ),
0
],
[
"Jim",
CreateDate( 1988, 1, 1 ),
0
]
] );
date = createDateTime( 2016, 3, 13, 17, 0, 0 );
totalAge = queryreduce( people, ( Any age = 0, Any row, Any rowNumber, Any recordset ) => {
return age + DateDiff( "yyyy", recordset.DOB, date );
} );
writeDump( totalAge );
Related
Last updated
Was this helpful?