I need to create a function in Mysql that should do the following things:
select 3 columns value from a table in the following way:
SELECT column1, column2, column3 FROM table WHERE id = value;
sort values of these columns, in order to know the greatest, the middle and the least.
- return the value: (greatest+middle-least)/3
My question is doublefold:
1) is there a way to obtain the middle value, as there are GREATEST() and LEAST()?
2) why the following function return always the following error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2); END $$ DELIMITER' at line 27
DELIMITER $$
CREATE FUNCTION abc_value(partita INT)
RETURNS DOUBLE DETERMINISTIC
BEGIN
DECLARE a, b, c, column1, column2, column3 DOUBLE;
DECLARE a CURSOR FOR SELECT GREATEST(column1, column2, column3) FROM table where id = value;
DECLARE c CURSOR FOR SELECT LEAST(column1, column2, column3) FROM table where id = value;
DECLARE column1 CURSOR FOR SELECT column1 FROM table where id = value;
DECLARE quotax CURSOR FOR SELECT column2 FROM table where id = value;
DECLARE quota2 CURSOR FOR SELECT column3 FROM table where id = value;
IF column1<> a THEN
IF column1<> b THEN
SET c = column1;
END IF;
END IF;
IF column2<> a THEN
IF column2<> b THEN
SET c = column2;
END IF;
END IF;
IF column3<> a THEN
IF column3<> b THEN
SET c = column3;
END IF;
END IF;
RETURN ROUND((a+b-c)/3),2);
END$$
DELIMITER;