题解 | #计算用户8月每天的练题数量#

计算用户8月每天的练题数量

http://www.nowcoder.com/practice/847373e2fe8d47b4a2c294bdb5bda8b6

题目:【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 

全部评论
我感觉有点化简为繁了
1 回复 分享
发布于 2023-12-12 21:01 浙江
方法一的where处可以用year_month,group by处可以直接写前面定义的别名day select extract(day from date) as day, count(question_id) as question_cnt from question_practice_detail where extract(year_month from date)='202108' group by day
点赞 回复 分享
发布于 2024-05-28 10:45 内蒙古
为什么要加having day is not null
点赞 回复 分享
发布于 2022-12-14 10:43 浙江

相关推荐

10-29 18:20
济南大学 Java
用微笑面对困难:他不是人事吗,怎么净特么不干人事
点赞 评论 收藏
分享
11-19 18:44
已编辑
成都理工大学 Java
程序员花海:我面试过100+校招生,大厂后端面试不看ACM,竞赛经历含金量低于你有几份大厂实习 这个简历整体来看不错 可以海投
如何写一份好简历
点赞 评论 收藏
分享
评论
38
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务