题解 | #月总刷题数和日均刷题数#

月总刷题数和日均刷题数

http://www.nowcoder.com/practice/f6b4770f453d4163acc419e3d19e6746

简单说一下吧,我看了发的解题思路也讲解的很详细,但是都有一个关键错误 SQL_ERROR_INFO: "Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'practice_record.submit_time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"

该错误很多人并未解决,关键在于分组的错误,因为对月份进行分组之后

day(last_day(submit_time))

该部分同样是分组的结果,很多人并没有分析出来,因此需要对该分组的结果进行取均值,也就是该月的天数,最终结果就出来了!

replace(left(submit_time,7),'-','') as submit_month,
count(*) as month_q_cnt,
round(count(*) / avg(day(last_day(submit_time))) ,3) as avg_day_q_cnt
from practice_record where score is not null 
and year(submit_time) = '2021'
group by replace(left(submit_time,7),'-','')
union all 
select 
'2021汇总' as submit_month,
count(*) as month_q_cnt,
round(count(*) /max(day(last_day(submit_time))) ,3) as avg_day_q_cnt -- /30 会不通过用例
from practice_record where score is not null 
and year(submit_time) = '2021' 
order by submit_month ;

alt

全部评论
哦~~~~~明白了,楼主真棒~~~~ 因为day()不是个聚合函数,所以返回的其实是submit_time里出现的N条记录所涉及月份当月天数的N行记录,但是分子是count(question_id)是个聚合函数。 其他解题思路里更合适的说法应该是把any_value写在day()函数之前,即 any_value(day(last_day(submit_time)))
5 回复 分享
发布于 2022-11-17 12:20 江苏
对day(last_day(submit_time))用函数max()或min()也可以通过测试,不一定是avg()
2 回复 分享
发布于 2022-08-04 10:37
还是不太懂为什么
1 回复 分享
发布于 2022-09-30 20:32 广东
为什么这样就不报错呢?day(last_day(submit_time))也不在答主这个分组里啊
点赞 回复 分享
发布于 2022-09-20 15:33 浙江
什么意思呀 没看懂
点赞 回复 分享
发布于 2022-09-01 13:54 北京
分析的好啊, 很多题解都没说这点, 而且因为这个好多题解都不能AC了.
点赞 回复 分享
发布于 2022-05-28 14:26
妙啊
点赞 回复 分享
发布于 2022-04-15 15:11

相关推荐

06-10 21:15
门头沟学院 Java
宁阿:好多这种没🧠的公司,他们估计都不知道毕业的人不能给安排实习岗
实习吐槽大会
点赞 评论 收藏
分享
评论
93
4
分享

创作者周榜

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