题解 | #近三个月未完成试卷数为0的用户完成情况#

近三个月未完成试卷数为0的用户完成情况

http://www.nowcoder.com/practice/4a3acb02b34a4ecf9045cefbc05453fa

一开始的错误:

SELECT uid, COUNT(submit_time) exam_complete_cnt FROM exam_record
WHERE TIMESTAMPDIFF(MONTH, start_time, 
                    (SELECT MAX(start_time) FROM exam_record GROUP BY uid)) < 3
GROUP BY uid
HAVING COUNT(submit_time) = COUNT(start_time)
ORDER BY exam_complete_cnt DESC, uid DESC;

报错'Subquery returns more than 1 row'。SELECT MAX(start_time) FROM exam_record GROUP BY uid)不能这么写,因为他返回了多行。

正确答案:

SELECT uid, COUNT(submit_time) exam_complete_cnt FROM 
(SELECT uid,start_time,submit_time, 
 dense_rank() over (PARTITION BY uid ORDER BY DATE_FORMAT(start_time,'%Y%m') DESC) rk
FROM exam_record) t1
WHERE t1.rk<=3
GROUP BY uid
HAVING COUNT(submit_time) = COUNT(start_time)
ORDER BY exam_complete_cnt DESC, uid DESC;

要点是窗口函数的使用

全部评论

相关推荐

joe2333:怀念以前大家拿华为当保底的日子
点赞 评论 收藏
分享
ProMonkey2024:5个oc?厉害! 但是有一个小问题:谁问你了?😡我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了(别的帖子偷来的,现学现卖😋)
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务