for is GROUP BY in disguise
The
for
statement behaves like the
SQL GROUP BY
clause where the expression in the parantheses (here:
t['name']
) is the value on which the other elements are grouped (and/or aggregated).
[ out : csv(
name,
total_length,
cnt_ways,
min_length,
max_length,
avg_length,
all_lengths
)];
{{geocodeArea: Pfungen }};
way[highway](area);
for (t['name']) {
make Beispiel
name = _.val,
total_length = sum(length()), // Aggregate all lengths
cnt_ways = count(ways) ,
min_length = min(length()),
max_length = max(length()),
avg_length = sum(length()) / count(ways), // There is no function avg()
all_lengths = set(length()) // Concatenate the individual lengths with semicolons.
;
out;
}
Of course, we can use a constant expression as group by expression so that we can, for example, calculate the total length of multiple ways:
[out:csv(
length,
cnt,
name
)];
(
way(228531783); // Length = 60.520
way(228533515); // Length = 38.373
) -> .ways;
for.ways (1) { // Group by constant value -> get aggregate for all elements
make x
cnt = count(ways),
length = sum(length());
out;
}