Whatever message this page gives is out now! Go check it out!
Object QueryReduce(Object query , UDFMethod reduceFunc [, Object initialValue])Parameter | Description |
query | (Required) The query to be iterated over. |
reduceFunc | (Required) The reduce function to be called with each row of the query. |
initialValue | (Optional) The value to be passed to reduce function for the first row. |
<cfscript>
myQuery = queryNew("id,name,amount","Integer,Varchar,Integer",
[
{id=1,name="One",amount=15},
{id=2,name="Two",amount=18},
{id=3,name="Three",amount=32},
{id=3,name="Four",amount=35}
]);
addAges=QueryReduce(myQuery,function(sum,obj){
sum=sum+obj.amount
return sum
},0)
writeOutput("The sum of all amount is: " & addAges)
</cfscript><cfscript>
qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"};
sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions);
temp = "";
titles = QueryReduce(sampleQuery, function(temp, element){
temp = temp?:"";
temp = temp & element.TITLE & " | ";
return temp;
});
writeOutput(titles);
</cfscript><cfscript>
myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"});
myArray=ArrayNew(1);
// Populate myArrray with random values between 25-45
for (i=1;i<=myResult.recordcount;i++){
myArray[i]=randrange(25,45);
}
// Add column Ages and populate with values of myArray
QueryAddColumn(myResult,"Ages","integer",myArray);
// Use reduce function to calculate the average age of all persons in the recordset
// Initialize age in closure
status=myResult.reduce(function(age=0,col){
return age+col.Ages/myResult.recordcount;
});
WriteOutput(status);
</cfscript>