I'm trying to script a mysql function to calculate my percentiles. My code :
CREATE DEFINER=`root`@`localhost` FUNCTION `percentile`(tbl_name VARCHAR(255),col_name VARCHAR(255)) RETURNS double(10,2)
READS SQL DATA
DETERMINISTIC
BEGIN
RETURN CONCAT('SELECT `',col_name,'` FROM
(SELECT t.*, @row_num :=@row_num + 1 AS row_num FROM `',tbl_name,'` t,
(SELECT @row_num:=0) counter ORDER BY `',col_name,'`)
temp WHERE temp.row_num = ROUND (0.75* @row_num);');
END
When I try to call it I get the error :
Error Code: 1265. Data truncated for column schema.percentile('table', 'column')
I can't figure out the source of the error. Any solutions ?