Aggregate FunctionsStatistical
avgif
Calculates the average of values in the group for which the predicate evaluates to true.
Equivalent to avg(iff(predicate, val, ...)) but registered as an aggregate
so it composes with summarize directly.
Like avg, the value argument must be numeric. Returns real.
Syntax
avgif(val, predicate)Parameters
Prop
Type
Returns: real
Examples
Example 1
datatable(warrior:string, voyages:long, elite:bool)[
"Ragnar", 42, true,
"Bjorn", 31, true,
"Floki", 12, false,
"Ivar", 35, true,
"Harald", 25, false
]
| summarize avgif(voyages, elite)| avgif_voyages (real) |
|---|
| 36.0 |
Example 2
datatable(clan:string, voyages:long, completed:bool)[
"Lothbrok", 42, true,
"Lothbrok", 31, false,
"Fairhair", 25, true,
"Fairhair", 12, true
]
| summarize avgif(voyages, completed) by clan| clan (string) | avgif_voyages (real) |
|---|---|
| Fairhair | 18.5 |
| Lothbrok | 42.0 |