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

月总刷题数和日均刷题数

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

ONLY_FULL_GROUP_BY报错原因 -- 详解!!!

很多解题里面都笼统的说了一句,如果你报错了1055,那你就加个any_value,但没有解释为什么,可能也是嫌麻烦吧,毕竟这里其实牵扯的是聚合函数和非聚合函数怎么一起进行计算(一般来说,我们是通过join或者over()来解决)

[42000][1055] Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'testrun.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() 函数返回的是N条记录,即表里有多少行记录,day()就会返回多少行记录。它不是一个聚合函数!

select
	date_format(submit_time, '%Y%m') as submit_month,
	day(last_day(submit_time))
from practice_record
where year(submit_time)=2021

然后我们再跑聚合函数count()这个代码:

select
    date_format(submit_time, '%Y%m') as submit_month,
    count(question_id)
from practice_record
where year(submit_time)=2021
group by submit_month

所以发现没有,两者直接放一起的话,怎么通过submit_month 来group by????

所以给day()函数加了个any_value(),就是说我还是照常按照submit_month来分组,但是我只是在day()函数结果里按照月份随便取个值

全部评论

相关推荐

06-17 21:57
门头沟学院 Java
白友:噗嗤,我发现有些人事就爱发这些,明明已读不回就行了,就是要恶心人
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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