SELECT * FROM A
INNER JOIN B
ON A.id = B.A_id
INNER JOIN C
ON B.B_id = C.id
GROUP BY A.id
HAVING C.code = 1
var a = [], // Array
i = 0, x,
current = a[0] + a[1],
max = current;
for (i = 0; i in a; i++) {
current = a[i];
for (x = i + 1; x in a; x++) {
current = current + a[x];
if (current > max) {
max = current;
}
}
}
console.log('Result:', max);