select
date_format(submit_time, '%Y%m') submit_month,
any_value(count(question_id)) month_q_cnt,
any_value(
round(count(question_id) / day(LAST_DAY(submit_time)), 3)
) avg_day_q_cnt
from
practice_record
where
date_format(submit_time, '%Y') = '2021'
group by
submit_month
-- 不需要去重
union all
select
'2021汇总' as submit_month,
count(*) as month_q_cnt,
round(count(*) / 31, 3) as avg_day_q_cnt
from
practice_record
where
score is not null
and year(submit_time) = '2021'
order by
submit_month;
分组汇总,可以考虑with rollup
,跟在group by
之后的第一个字段将被汇总。
取 day(LAST_DAY(submit_time))
之后再使用聚合函数,是因为数据有多条,我们只取一条,同理使用avg
也可以。
select any_value(coalesce (DATE_FORMAT(submit_time, '%Y%m'),"2021汇总")) submit_month,
any_value(count(submit_time)) month_q_cnt,
any_value(round(count(question_id)/MAX(day(LAST_DAY(submit_time))), 3)) avg_day_q_cnt
from practice_record
where YEAR(submit_time)='2021'
group by DATE_FORMAT(submit_time, "%Y%m") -- 这里不能用别名
with rollup ;
这里的coalesce
可以用ifnull
替代。
select any_value(ifnull (DATE_FORMAT(submit_time, '%Y%m'),"2021汇总")) submit_month,
any_value(count(submit_time)) month_q_cnt,
any_value(round(count(question_id)/MAX(day(LAST_DAY(submit_time))), 3)) avg_day_q_cnt
from practice_record
where YEAR(submit_time)='2021'
group by DATE_FORMAT(submit_time, "%Y%m") -- 这里不能用别名
with rollup ;