题解 | #SQL31 未完成率较高的50%用户近三个月答卷情况#

未完成率较高的50%用户近三个月答卷情况

http://www.nowcoder.com/practice/3e598a2dcd854db8b1a3c48e5904fe1c

select uid,
			 date_format(start_time,'%Y%m') as start_month,
			 count(start_time) as total_cnt,
			 count(submit_time) as complete_cnt
from ( -- 6、7级用户在近三个月有试卷作答记录的用户 #uid集合为1001、1002
			select uid,
						 start_time,
						 submit_time
		  from (
						select er.uid,
									 start_time,
									 submit_time,
									 dense_rank() over(partition by uid order by date_format(start_time,'%Y%m') desc) as recent_month
						from exam_record as er
						left join user_info as ui
						on ui.uid=er.uid
						where level>=6  #这一步筛选掉了1003
						) as recent_table
			where recent_month<=3
			) as final_table
where uid in ( -- SQL试卷上未完成率较高的50%用户uid集合 -- #uid集合为1002、1003
							select uid
							from (
										select uid,
													 total_cnt,
													 incomplete_rate,
													 rank_num
										from (
													select uid,
																 incomplete_cnt,
																 total_cnt,
																 incomplete_cnt/total_cnt as incomplete_rate,
																 row_number() over(order by incomplete_cnt/total_cnt desc) as rank_num
													from (
																select uid,
																			 count(if(submit_time is null,1,null)) as incomplete_cnt,
																			 count(start_time) as total_cnt
																from exam_record as er
																left join examination_info as ei
																on ei.exam_id=er.exam_id
																where tag='SQL'
																group by uid
																) as inner_table
												 ) as table1
									 where rank_num<=ceiling((select count(distinct uid)/2 from exam_record))
									   ) as connect_table
								 )
group by uid,start_month
order by uid asc,start_month asc;

全部评论

相关推荐

Java面试先知:我也是和你一样的情况,hr 说等开奖就行了
点赞 评论 收藏
分享
八极星:有什么不能问的,(/_\),这又不是多珍贵的机会,你有什么可失去的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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