I have a table with an enum field. I need to calculate average value of this field and show it as one of enum elements.
I'm getting the average like this: SELECT AVG(val) FROM test;
It works fine. But then I need to get one of enum values back by this index. To get index I'm using ROUND
. But I don't know what to do next.
Is it possible?
Is there some other approach?
Example
Create a table:
CREATE TABLE test (
id INT AUTO_INCREMENT PRIMARY KEY,
val ENUM('bad', 'good', 'perfect')
);
Fill the table:
INSERT INTO test(val) VALUES('bad'), ('bad'), ('good'), ('bad'), ('perfect'), ('perfect'), ('good');
Select query to be fixed somehow:
SELECT AVG(val) average_value_ind, ROUND(AVG(val)) closest_value_ind FROM test;
In this example the average value should be good