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

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

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

题意明确:

2021年8每天用户练习题目的数量


问题分解:

  • 限定条件:2021年8月,写法有很多种,比如用year/month函数的year(date)=2021 and month(date)=8,比如用date_format函数的date_format(date, "%Y-%m")="202108"
  • 每天:按天分组group by date
  • 题目数量:count(question_id)

细节问题:

  • 表头重命名:as
  • 输出示例中每天的字段只取了几号,要去掉年月,用day函数即可

完整代码:

select
    day(date) as day,
    count(question_id) as question_cnt
from question_practice_detail
where month(date)=8 and year(date)=2021
group by date
全部评论
where month(date)=8 and year(date)=2021 换成 where left(date,7)='2021-08' 也可以
7 回复 分享
发布于 2022-03-31 14:36
使用date_format闭个坑,“-”别忘记了 where date_format(q.date, '%Y-%m') = "2021-08"
6 回复 分享
发布于 2022-08-06 18:04
select day(date) as day,count(1) as question_cnt from question_practice_detail where date between '2021-08-01' and '2021-08-31' group by day; 这样可以, 真实环境如果date带索引, 在条件表达式左右带函数会导致全表扫描.
4 回复 分享
发布于 2022-05-17 14:25
答案为啥group和where用了,不是group和having连用嘛
4 回复 分享
发布于 2022-03-11 18:18
date_format(date, "%Y-%m")="202108" 需要改成date_format(date, "%Y-%m")="2021-08" 或者date_format(date, "%Y%m")="202108" 以匹配date_format的格式
2 回复 分享
发布于 2023-04-10 15:19 江苏
select substr(date,-2) day,count(question_id) question_cnt from question_practice_detail group by date having date like '2021-08%'
2 回复 分享
发布于 2022-04-16 16:46
select day(date) day, question_cnt from ( select date, count(*) as question_cnt from question_practice_detail where date >= '2021-08-01' and date <= '2021-08-31' group by date ) m -- 性能最高的写法
1 回复 分享
发布于 2022-10-30 01:37 陕西
这位朋友严谨啊,我漏掉了2021
1 回复 分享
发布于 2022-05-15 22:20
select day(date) as day, count(result) as question_cnt from question_practice_detail group by date having date_format(date, "%Y-%m")="2021-08" select day(date) as day, count(result) as question_cnt from question_practice_detail group by date having year(date)=2021 and month(date)=8
1 回复 分享
发布于 2022-02-09 16:33
select distinct day(date) as day, count(question_id) over(partition by date) as question_cnt from question_practice_detail where year(date)=2021 and month(date)=08;
1 回复 分享
发布于 2022-01-26 15:10
用like 或者where中对字段使用公式会降低查询效率直接用where date between '2021-08-01' and '2021-08-31'
点赞 回复 分享
发布于 2024-04-17 14:56 福建
日期不是都是字符吗?为什么不用加‘’啊
点赞 回复 分享
发布于 2024-03-14 10:54 内蒙古
sql真的给你玩明白了
点赞 回复 分享
发布于 2024-03-02 15:09 上海
date_format(date,"%Y-%m") = "2021-08"
点赞 回复 分享
发布于 2023-09-19 19:52 河北
正确的date_forma函数应为 date_format(date, "%Y%m")="202108"
点赞 回复 分享
发布于 2023-09-17 14:52 北京
select day(date) as day , count(question_id) as question_cnt from question_practice_detail where date_format(date,'%Y-%m') group by day 为什么这样也能通过?
点赞 回复 分享
发布于 2022-12-31 13:34 澳大利亚
select day(date) as day,count(question_id) as question_cut from question_practice_detail group by day(date) where month(date)=8 and year(date)=2021;为什么group by 在前面不行 各位大佬
点赞 回复 分享
发布于 2022-11-09 14:04 安徽
把where month(date)=8 and year(date)=2021换成 where date like '%-08-%'也可以
点赞 回复 分享
发布于 2022-11-01 16:41 广东
group by date 和group by day 一样的吧?
点赞 回复 分享
发布于 2022-10-06 14:03 广东
select substring_index(date,'-',-1) day, count(question_id) question_cnt from question_practice_detail where date like '2021-08%' group by day
点赞 回复 分享
发布于 2022-09-23 20:58 山西

相关推荐

一条从:又想干活还想拿工资,什么好事都让你占了
点赞 评论 收藏
分享
评论
321
68
分享

创作者周榜

更多
牛客网
牛客企业服务