mysql - Multiple aggregations in a single SQL statement -
i have sql table following structure:
id | datetime | value --------------------- what results following structure:
id | value_avg_overall | value_avg_last_month | value_avg_last_week ------------------------------------------------------------------- currently using join, constructing query similar one:
select * ( select_overall inner join select_last_month on select_overall.id = select_last_month.id ) inner join select_last_week on select_last_week.id = select_last_month.id; i have simplified query make more readable, select_* statements simple select statements using group by avg() respective time ranges.
is effective way perform multiple aggregations , return of them in single result set in mysql?
this called conditional aggregation:
select id, avg(value) value_avg_overall, avg(case when datetime > date_sub(curdate(), interval 1 month) value end) value_avg_last_month, avg(case when datetime > date_sub(curdate(), interval 1 week) value end) value_avg_last_week mytable group id;
Comments
Post a Comment