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
/*--Output--
12
*/
CREATE OR REPLACE AGGREGATE FUNCTION `your_project_id.your_dataset_id.weighted_average`(`col` float64, `weight` float64)
RETURNS float64 AS (sum(col * weight) / sum(weight))
OPTIONS ( description = '''Finds the weighted average of a column <value>.''')
SQL User Defined Aggregate Function (SQL UDAF)
See something wrong? Contact us or report an issue on Github.