当前位置:网站首页>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.
边栏推荐
- 使用SourceTree添加SSH公钥并克隆码云项目(笔记整理篇)
- 【燃】是时候展现真正的实力了!一文看懂2022华为开发者大赛技术亮点
- The first day of the real in CSDN
- [1413. Stepwise summation to get the minimum value of positive numbers]
- 网络——IPv6 vs IPv4
- 初识C语言(1)
- 2022钉钉杯A题思路及代码:银行卡电信诈骗危险预测
- character rhombus code
- 「我觉得AI领域乙烷」网友:你说的太多了,让AI来总结一下
- SQL抖音面试题:送你一个万能模板,要吗?(重点、每个用户每月连续登录的最大天数)
猜你喜欢
随机推荐
领先实践|全球最大红酒App如何用设计冲刺创新vivino模式
2022-08-09日报:做学术OR去公司 ? 想通这点,治好 AI 打工人的精神内耗
Why does a four-byte float represent a wider range than an eight-byte long
投入C语言
Three ways to find prime numbers
Redis learning (1. Data structure in redis)
良匠-手把手教你写NFT抢购软(三)
August 9, 2022: Build .NET apps in C# -- use the Visual Studio Code debugger to interactively debug .NET apps (won't, fail)
良匠-手把手教你写NFT抢购软(一)
网络——彻底搞懂数据时延的相关计算
如何判断闰年
Chapter 1: GEE and GEEMAP
计组——大端方式和小端方式相关题目
总结了 110+ 公开专业数据集
时间日期格式工具类
2022年8月9日:用C#生成.NET应用程序--使用 Visual Studio Code 调试器,以交互方式调试 .NET 应用(不会,失败)
IDEA中操作数据库 以MySQL为例,可以放弃Navicat了
CompletableFuture异步线程优化代码
Collection of DP Optimization Methods
5. Visualizing Geospatial Data