mardi 4 août 2015

MySQL conditional select else aggregate statement

I have several columns for which I'm selecting an average AVG(), some of which are quite small. I want to aggregate anything less than 0.01 to the result of "other".

Something like:

SELECT 
  AVG(data_1) as data_1, 
  AVG(data_2) as data_2, 
  AVG(data_3) as data_3, 
  AVG(data_4) as data_4, 
  AVG(data_5) as data_5
FROM my_table AS my_results;
SET @other = 0;
IF my_results.data_1 > 0.01 THEN
 SET my_results.data_1 my_results.data_1
ELSE 
 @other := @other + my_results.data_1
 UNSET my_results.data_1
[... repeat for data_2 - data_5 ]
RETURN my_results

But that's as far as I've gotten. I'd want any data not more then 0.01 to be removed from the results after being aggregated to @other. Even a good pointer in the right direction is greatly appreciated!

Thanks

Aucun commentaire:

Enregistrer un commentaire