query.reduce()

Iterates over every row of the given query and calls the closure with that row.

This function will reduce the query to a single value and will return this value.

query.reduce( closure=function, initialValue=object )

Returns: any

Argument Description
closure
function, required

function/closure that implements the following constructor

function(any result, struct row[, number rowNumber, query query]){ return result;}.

Alias: function, callback, udf

initialValue
object, optional

initial value passed as part of the first closure call

Alias: initial, initalValue

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 ]
	]
	);
	dump( var=people, label="people - original query" );
	/* Output:
	| name | dob                 | age |
	------------------------------------
	| Susi | 1970-01-01 00:00:00 | 0   |
	| Urs  | 1995-01-01 00:00:00 | 0   |
	| Fred | 1960-01-01 00:00:00 | 0   |
	| Jim  | 1988-01-01 00:00:00 | 0   |
	*/
<span class="c">// initialise age as a second argument</span>
<span class="nv">totalAge</span> <span class="o">=</span> <span class="nf">people.reduce</span><span class="p">(</span> <span class="nf">function</span><span class="p">(</span><span class="nv">age</span><span class="p">,</span> <span class="nv">row</span><span class="p">,</span> <span class="nv">rowNumber</span><span class="p">,</span> <span class="nv">recordset</span> <span class="p">){</span>
    <span class="nv">return</span> <span class="nv">age</span> <span class="o">+</span>  <span class="nf">DateDiff</span><span class="p">(</span> <span class="s1">&#39;yyyy&#39;</span><span class="p">,</span> <span class="nv">recordset.dob</span><span class="p">,</span> <span class="nf">Now</span><span class="p">()</span> <span class="p">);</span>
<span class="p">},</span><span class="m">0</span><span class="p">);</span>
<span class="nf">Dump</span><span class="p">(</span> <span class="k">var</span><span class="o">=</span><span class="nv">totalAge</span><span class="p">,</span> <span class="nv">label</span><span class="o">=</span><span class="s1">&#39;people - total age&#39;</span> <span class="p">);</span>
<span class="o">/*</span> <span class="nv">Output</span><span class="p">:</span>
    <span class="nv">totalAge</span> <span class="o">=</span> <span class="m">167</span>
<span class="o">*/</span>
<span class="c">// initialise age in closure</span>
<span class="nv">totalAge</span> <span class="o">=</span> <span class="nf">people.reduce</span><span class="p">(</span> <span class="nf">function</span><span class="p">(</span><span class="nv">age</span><span class="o">=</span><span class="m">0</span><span class="p">,</span> <span class="nv">row</span><span class="p">,</span> <span class="nv">rowNumber</span><span class="p">,</span> <span class="nv">recordset</span> <span class="p">){</span>
    <span class="nv">return</span> <span class="nv">age</span> <span class="o">+</span>  <span class="nf">DateDiff</span><span class="p">(</span> <span class="s1">&#39;yyyy&#39;</span><span class="p">,</span> <span class="nv">recordset.dob</span><span class="p">,</span> <span class="nf">Now</span><span class="p">()</span> <span class="p">);</span>
<span class="p">});</span>
<span class="c">// Here you can get NULL value if there is no recordset</span>
<span class="k">if</span><span class="p">(</span> <span class="nf">isNull</span><span class="p">(</span><span class="nv">totalAge</span><span class="p">)</span> <span class="p">)</span> <span class="p">{</span>
    <span class="nv">totalAge</span> <span class="o">=</span> <span class="m">0</span><span class="p">;</span>
<span class="p">}</span>
<span class="nf">dump</span><span class="p">(</span> <span class="k">var</span><span class="o">=</span><span class="nv">totalAge</span><span class="p">,</span> <span class="nv">label</span><span class="o">=</span><span class="s1">&#39;people - total age&#39;</span> <span class="p">);</span>
<span class="o">/*</span> <span class="nv">Output</span><span class="p">:</span> <span class="k">if</span> <span class="nv">recordset</span> <span class="nv">exists</span> <span class="k">else</span> <span class="m">0</span>
      <span class="nv">totalAge</span> <span class="o">=</span> <span class="m">167</span>
<span class="o">*/</span>

See also