weighted_average(col, weight)
Finds the weighted average of a column <value>.
CREATE OR REPLACE AGGREGATE FUNCTION `justfunctions.eu.weighted_average`(`col` float64, `weight` float64)
RETURNS float64 AS (sum(col * weight) / sum(weight))
OPTIONS ( description = '''Finds the weighted average of a column <value>.''')
Example
WITH sample AS (
select 10 as g, 1 as w
union all
select 13 as g, 2 as w
)
SELECT `justfunctions.eu.weighted_average`(g, w)
FROM sample
12