当前位置:网站首页>SQL trill interview: send you a universal template, to?(key, each user to log on to the maximum number of consecutive monthly)
SQL trill interview: send you a universal template, to?(key, each user to log on to the maximum number of consecutive monthly)
2022-08-09 18:20:00 【51CTO】
【面试题】
有一张“用户登陆记录表”,包含两个字段:用户id、日期.


【问题】查询2021年每个月,The maximum number of consecutive days each user can log in.
【解题步骤】
1. A universal template for running questions
在 《拼多多面试题:如何找出连续出现N次的内容?》Said to meet“连续问题”如何解决,And sent a universal template,模板使用的是 窗口函数解决连续问题.

2. 窗口函数
窗口函数lead使用方法:

默认值是指:当向上Nline or downNrow value,If it has exceeded the range of table rows and columns,This default value will be used as the return value of the function,若没有指定默认值,则返回Null.
窗口函数leadYou can get the last number of each fieldn个值,并生成新的一列.
And this question describes“The user logs in continuously”中的“连续”It can be understood that the user's current login date is one day away from the next login date of this month.
We can use window functions firstlead获取“The user's next login date in the current month”:



当“日期”is when the user logged in on the last day of the month,记录为“Last login date of the month”,如果不进行设置,将会返回Null,不利于理解.
从结果看,我们可以获得以下信息:
1)当“日期”与“The user's next login date in the current month”Only one day away,That is, the user's current login is a continuous login;
2)当“日期”与“The user's next login date in the current month”The difference is more than one day,That is, the user's current login is the last day of continuous login(It is also possible to log in for only one day);
3)当“The user's next login date in the current month”等于“Last login date of the month”,That is, the user's login is the last day of the month.
这样,It is possible to judge the continuous login of the user.
Next, we will solve the calculation of the number of consecutive login days for each user.
3. 子查询


There is a certain relationship between the number of consecutive login days of each user and the user login sequence,At this point, we can use a subquery to query the reading order of users in this month,使用窗口函数row_number:



可以看出,when the continuation is terminated,即:
1)“日期”与“The user's next login date in the current month”The difference is more than one day;
2)“The user's next login date in the current month”等于“Last login date of the month”;
两种情况.
After filtering these two cases out,The number of consecutive days a user has logged in is :The current login sequence minus the previous login sequence.



“The previous login sequence”为Null时,用0代替(使用coalesce函数),那么“Monthly login order”减去“The previous login sequence”This is the number of consecutive login days.
4. 汇总分析
最后获取“每个月,The maximum number of consecutive days each user can log in”,使用group by函数.



【本题考点】
1.考查对窗口函数的了解,要把 《猴子 从零学会SQL》The window function mentioned in the above can be solved4Class interview questions to remember;
2.Examine your understanding of subqueries;
3.Examine your knowledge of sequential problems,Universal templates can be applied.
边栏推荐
猜你喜欢
随机推荐
成为CTO,6个月被老板干死,我损失了1000万
开源星「001 号」落地 FlyFish,欢迎登陆赢神秘大礼包!
PHP completes missing dates in date ranges/returns missing dates
Base64工具类
2. Creating Interactive Maps
map和set容器
2022年深圳杯数学建模A题代码思路-- 破除“尖叫效应”与“回声室效应”,走出“信息茧房”
1. Introducing GEE and Geemap
Chapter 3: Use of GEE Data (3.4-3.11)
【服务器数据恢复】SAN LUN映射出错导致文件系统数据丢失的数据恢复案例
如何判断闰年
【燃】是时候展现真正的实力了!一文看懂2022华为开发者大赛技术亮点
ESP8266-Arduino编程实例-MQ-6异丁烷丙烷传感器驱动
四.数组传参
Nacos Jaspyt配置加密设置
IDEA启动缓慢原因(一)
std::uniform_real_distribution的一个bug引发的服务器崩溃
margin:auto实现盒子水平垂直居中
shopee引流方式有哪些,商家如何为自己店铺做引流?
六.数组越界问题引出对栈区内存的探索









