当前位置:网站首页>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.
边栏推荐
- 【Web渗透】信息收集篇——Google搜索引擎(二)
- 想通这点,治好 AI 打工人的精神内耗
- 【Chinese and English Catalog】Introduction
- Chapter 1: GEE and GEEMAP
- NFT+IDO预售代币合约模式系统开发
- character rhombus code
- Video chat source code - how to improve the quality of one-to-one live broadcast?
- 零基础爬虫regex练习「音乐抓取」
- 如何判断闰年
- Heap series_0x0A: 3 methods to solve the heap overflow problem at once
猜你喜欢

成为CTO,6个月被老板干死,我损失了1000万

图像几何校正

MySQL索引的B+树到底有多高?

August 9, 2022: Build .NET apps in C# -- use the Visual Studio Code debugger to interactively debug .NET apps (won't, fail)

Video chat source code - how to improve the quality of one-to-one live broadcast?

Anatomy of Storage Size, Value Range, and Output Format of Basic Data Types in C Language

C语言分支语句if,switch语句详细讲解

Detailed explanation of three pieces in C language

IDEA中操作数据库 以MySQL为例,可以放弃Navicat了

苹果开发者账号 申请 D-U-N-S 编号
随机推荐
转行做程序员,从月薪5k到30k,45岁测试员道出了一路的心酸
[1413. Stepwise summation to get the minimum value of positive numbers]
网络——TCP拥塞控制
网络——2021年大题解析
AVL树的插入操作
网络——数字数据编码
ESP8266-Arduino编程实例-MQ-4气体传感器驱动
std::uniform_real_distribution的一个bug引发的服务器崩溃
知识点滴 - 如何写项目总结
Access Characteristics of Constructor under Inheritance Relationship
Selenium的安装
character rhombus code
uniapp 项目搭建
2022年8月9日:用C#生成.NET应用程序--使用 Visual Studio Code 调试器,以交互方式调试 .NET 应用(不会,失败)
uni-app覆盖组件样式h5生效,微信小程序不生效的问题
四.数组传参
微服务框架笔记(1)
Chapter 4: Using Local Geospatial Data (4.6-4.14)
机器学习强基计划1-2:图文详解线性回归与局部加权线性回归+房价预测实例
投入C语言