php - Add attribute "on the fly" during mySQL query -
i have query looks this:
select avg(round({$cohort1}/{$cohort2} * 100)) overall_average, `category`, `value`, {$cohort1},{$cohort2},`group`, `quadrant`,round({$cohort1}/{$cohort2} * 100) index_value {$table};
this doesn't quite work when include average piece. returns 1 row. need return rows. if remove average piece, returns fine, don't have average calculations. separate query in php though.
however, realize need derive value of quadrant(a string) based on values of cohorts. these rules. problem have no idea how incorporate query.
if (the_value > overall_average , index_value > 100) `quadrant` = "priority one" elseif (the_value < overall_average , index_value > 100) `quadrant` = "potential" elseif (the_value > overall_average , index_value < 100) `quadrant` = "basics" elseif (the_value < overall_average , index_value < 100) `quadrant` = "ignore"
can me come complete query this? i'm open manipulating data in php after it's returned database if that's simpler.
you can use case clause:
select column1, column2, column2 alias,..., case when the_value > overall_average , index_value > 100 'priority one' ... when another_condition 'potential' ... else 'default value' end quadrant table
reference https://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#operator_case
Comments
Post a Comment