I want to write a function that calculates a simple trimmed mean calculation in MySQL. The function will (obviously) be an aggregate function. I am new to writing functions etc in MySQL so could do with some help.
The algorithm of the trimmed mean will be as follows (pseudocode):
CREATE AGGREGATE FUNCTION trimmed_mean(elements DOUBLE[], trim_size INTEGER)
RETURNS DOUBLE
BEGIN
-- determine number of elements
-- ensure that number of elements is greater than 2 * trim_size else return error
-- order elements in ASC order
-- chop off smallest trim_size elements and largest trim_size elements
-- calculate arithmetic average of the remaining elements
-- return arithmetic average
END
Can anyone help with how to write the function above correctly, for use with MySQL?