-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsumming_items_totals_on_match_v2.xml
69 lines (58 loc) · 2.12 KB
/
summing_items_totals_on_match_v2.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?xml version="1.0" encoding="UTF-8"?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
<meta>
<author>Paul Daniel (hapdaniel)</author>
<description>Sum of specified elements over items. Returns totals only</description>
<documentationURL></documentationURL>
</meta>
<bindings>
<select itemPath="" produces="JSON">
<urls>
<url></url>
</urls>
<inputs>
<key id="data" type="xs:string" paramType="variable" required="true"/>
<key id="eles" type="xs:string" paramType="variable" required="false"/>
</inputs>
<execute><![CDATA[
y.include("http://hapdaniel.pwp.blueyonder.co.uk/pdaniel/yql/json2.js");
var fields = eles.split(",");
var arr_len = fields.length;
var sums = new Array();
var totals = new Array();
var output = new Object;
item=0;
for (var i = 0; i < arr_len; i++) {
totals[fields[i]] = 0;
}
totals["yql_match"] = "";
var pdata = JSON.parse(data);
var match_key = pdata.items[0].yql_match;
for (var j = 0; j < pdata.items.length; j++) {
if (pdata.items[j].yql_match == match_key) {
for (var i = 0; i < arr_len; i++) {
ele = "pdata.items[" + j + "]." + fields[i];
totals[fields[i]] += Number(eval(ele));
}}
else {
totals["yql_match"] = match_key;
match_key = pdata.items[j].yql_match;
for (var i = 0; i < arr_len; i++) {
totals[fields[i]] = totals[fields[i]].toString();
}
sums.push(totals);
for (var i = 0; i < arr_len; i++) {
totals[fields[i]] = 0;
}
}
}
//totals.yql_count = pdata.items.length;
//pdata.items.push(totals);
//output = JSON.stringify(sums);
y.log(sums.toString);
response.object = JSON.parse(sums);
]]>
</execute>
</select>
</bindings>
</table>