题目:【2021】年【8】月每天用户练习【题目数量】。
分析:时间条件可用date between 2021-08-01 and 2021-08-31 或者 year(date)=2021 and month(date)=8;题目数量使用count函数;截取日期函数用extract(day from date)。
方法一:常规解法
SELECT EXTRACT(day from date) day, count(*) question_cnt from question_practice_detail where year(date) = 2021 and month(date) = 8 group by EXTRACT(day from date)
方法二:case表达式
SELECT case when year(date) = 2021 and month(date) = 8 then extract(day from date) else null end as day, count(*) question_cnt from question_practice_detail group by day having day is not null方法三:IN+子查询
SELECT EXTRACT(day from date) day, COUNT(*) question_cnt from question_practice_detail where date in ( select date from question_practice_detail where YEAR(date) = 2021 and MONTH(date) = 8) group by day