Reputation: 8167
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
Upvotes: 0
Views: 51
Reputation: 136
You can not change number of selected columns during the query, however, you can set values to null if avg < 0.01 Here an example (I'm sure, it could be written in better way)
SELECT
if(AVG(d1) < 0.01, null, AVG(d1)) as data_1,
if(AVG(d2) < 0.01, null, AVG(d2)) as data_2,
if(AVG(d3) < 0.01, null, AVG(d3)) as data_3,
if(AVG(d4) < 0.01, null, AVG(d4)) as data_4,
if(AVG(d5) < 0.01, null, AVG(d5)) as data_5,
if(AVG(d1) < 0.01, AVG(d1), 0) +
if(AVG(d2) < 0.01, AVG(d2), 0) +
if(AVG(d3) < 0.01, AVG(d3), 0) +
if(AVG(d4) < 0.01, AVG(d4), 0) +
if(AVG(d5) < 0.01, AVG(d5), 0)
FROM my_table AS my_results, (select @other := 0) o
Upvotes: 1