当前位置:网站首页>五大常考SQL面试题
五大常考SQL面试题
2022-06-22 05:46:00 【Begin to change】
目录
一、找出连续7天登陆,连续30天登陆的用户(小红书笔试,电信云面试),最大连续登陆天数的问题 --窗口函数
三、计算除去部门最高工资,和最低工资的平均工资(字节跳动面试)--窗口函数
四、留存的计算,和累计求和的计算 --窗口函数,自联结(pdd面试)
一、找出连续7天登陆,连续30天登陆的用户(小红书笔试,电信云面试),最大连续登陆天数的问题 --窗口函数
1、先创建表以及导入数据
create table login(
user_id int comment '用户id',
access_time datetime comment '访问时间',
page_id int comment '页面id',
dt date comment '登陆日期'
);
insert into login values
(1, '2021-06-01 11:13:15', 10, '2021-06-01'),
(1, '2021-06-02 11:13:15', 10, '2021-06-02'),
(1, '2021-06-03 11:13:15', 10, '2021-06-03'),
(1, '2021-06-04 11:13:15', 10, '2021-06-04'),
(1, '2021-06-05 11:13:15', 10, '2021-06-05'),
(1, '2021-06-06 11:13:15', 10, '2021-06-06'),
(1, '2021-06-07 11:13:15', 10, '2021-06-07'),
(2, '2021-06-01 11:13:15', 10, '2021-06-01'),
(2, '2021-06-03 11:13:15', 10, '2021-06-03'),
(2, '2021-06-04 11:13:15', 10, '2021-06-04'),
(2, '2021-06-05 11:13:15', 10, '2021-06-05'),
(3, '2021-06-01 11:13:15', 10, '2021-06-01'),
(3, '2021-06-07 11:13:15', 10, '2021-06-07'),
(3, '2021-06-08 11:13:15', 10, '2021-06-08'),
(3, '2021-06-09 11:13:15', 10, '2021-06-09'),
(3, '2021-06-10 11:13:15', 10, '2021-06-10'),
(3, '2021-06-11 11:13:15', 10, '2021-06-11'),
(3, '2021-06-12 11:13:15', 10, '2021-06-12'),
(3, '2021-06-13 11:13:15', 10, '2021-06-13'),
(4, '2021-06-01 11:13:15', 10, '2021-06-01'),
(4, '2021-06-03 11:13:15', 10, '2021-06-03'),
(4, '2021-06-05 11:13:15', 10, '2021-06-05'),
(4, '2021-06-07 11:13:15', 10, '2021-06-07'),
(4, '2021-06-09 11:13:15', 10, '2021-06-09'),
(4, '2021-06-11 11:13:15', 10, '2021-06-11'),
(5, '2021-06-01 11:13:15', 10, '2021-06-01'),
(5, '2021-06-07 11:13:15', 10, '2021-06-07'),
(5, '2021-06-08 11:13:15', 10, '2021-06-08'),
(5, '2021-06-09 11:13:15', 10, '2021-06-09'),
(5, '2021-06-11 11:13:15', 10, '2021-06-11'),
(5, '2021-06-12 11:13:15', 10, '2021-06-12'),
(5, '2021-06-13 11:13:15', 10, '2021-06-13');2、思路
①先将用户按照id进行分组,然后再用窗口函数进行排序
SELECT *,ROW_NUMBER() over (PARTITION by user_id order by user_id)FROM login where month(dt) = 6
②将日期与排名做差 (如果用户是连续登陆,那么差值日期的结果是一样的)
select *,date_sub(dt,INTERVAL ranking day) diff from (SELECT *,ROW_NUMBER() over (PARTITION by user_id order by dt) ranking FROM login where month(dt) = 6) t③根据用户与时间差分类,统计出现的次数,如果次数在7日以上,则是想要的结果
select user_id ,count(*) from
(select *, date_sub(dt, interval ranking day) diff from
(select *, row_number() over(partition by user_id order by dt) ranking from login where month(dt)=6) as t) as t1
group by user_id, diff having count(*) >= 7;
二、求连续点击三次的用户数,而且中间不能有别人的点击
a表记录了点击的流水信息,包括用户id ,和点击时间
row_number() over(order by click_time) as rank_1 得到rank_1为 1 2 3 4 5 6 7
row_number() over(partition by usr_id order by click_time) 得到rank_2 为 1 2 1 3 4 5 6
rank_1- rank2 得到diff 为 0 0 2 1 1 1 1这时我们发现只需要对diff进行分组计数大于3个,就是连续点击大于三且中间没有其他人点击的用户
select distinct usr_id
from
(
select *, rank_1- rank2 as diff
from
(
select *,
row_number() over(order by click_time) as rank_1
row_number() over(partition by usr_id order by click_time) as rank_2
from a
) b
) c
group by diff,usr_id
having count(diff) >=3
三、计算除去部门最高工资,和最低工资的平均工资(字节跳动面试)--窗口函数
emp 表
id 员工 id ,deptno 部门编号,salary 工资
核心是使用窗口函数降序和升序分别排一遍就取出了最高和最低。
select a.deptno,avg(a.salary)
from
(
select *, rank() over( partition by deptno order by salary ) as rank_1
, rank() over( partition by deptno order by salary desc) as rank_2
from emp
) a
group by a.deptno
where a.rank_1 >1 and a.rank_2 >1 四、留存的计算,和累计求和的计算 --窗口函数,自联结(pdd面试)
手机中的相机是深受大家喜爱的应用之一,下图是某手机厂商数据库中的用户行为信息表中部分数据的截图

现在该手机厂商想要分析手机中的应用(相机)的活跃情况,需统计如下数据:
需要获得的数据的格式如下:

select d.a_t,count(distinct case when d.时间间隔=1 then d.用户id
else null
end) as 次日留存数,
count(distinct case when 时间间隔=1 then d.用户id
else null
end) /count(distinct d.用户id) as 次日留存率,
count(distinct case when d.时间间隔=3 then d.用户id
else null
end) as 3日留存数 ,
count(distinct case when 时间间隔=3 then d.用户id
else null
end) /count(distinct d.用户id) as 3日留存率,
count(distinct case when d.时间间隔=7 then d.用户id
else null
end) as 7日留存数 ,
count(distinct case when 时间间隔=7 then d.用户id
else null
end) /count(distinct d.用户id) as 7日留存率
from
(select *,timestampdiff(day,a_t,b_t) as 时间间隔
from (select a.`用户id`,a.登陆时间 as a_t ,b.登陆时间 as b_t
from 登录信息 as a
left join 登录信息 as b
on a.`用户id`=b.`用户id`
where a.应用名称= '相机' AND b.应用名称='相机') as c) as d
group by d.a_t;
边栏推荐
- n个整数的无序数组,找到每个元素后面比它大的第一个数,要求时间复杂度为O(N)
- vscode极简安装教程
- W800芯片平台进入OpenHarmony主干
- 402 string (Title: Sword finger offer58 ii. left rotation string, 28. implementation of strstr(), 459 Repeated substrings)
- 【雲計算重點複習】
- Use of idea plug-in EASYCODE
- PID笔记
- reduce_sum()中的reduction_indices
- [Examen des points clés de l'informatique en nuage]
- Single precision, double precision and precision (Reprint)
猜你喜欢

单球机器人动力学与控制研究

Signal output library

Ptrade trading program code - from zero to firm offer 19

C language pointer (Advanced)

触 发 器

MinGW download and installation

JTAG interface

MFC TabCtrl 控件修改標簽尺寸

400 hash table (1. sum of two numbers, 454. sum of four numbers II, 383. ransom letter)

3D asset optimization and vertex data management for performance optimization
随机推荐
PID notes
【技术随记】
单细胞论文记录(part14)--CoSTA: unsupervised convolutional neural network learning for ST analysis
使用SystemVerilog门模型描述的组合逻辑
Vscode minimalist installation tutorial
Improve your game‘s performance
idea插件EasyCode的使用
Server PHP related web page development environment construction
电脑卡顿怎么办?
Using SystemVerilog to describe a state machine
性能优化最佳实践之缩减游戏大小
RGB及sRGB与XYZ坐标转换
生信文献学习(part1)--PRECISE: a ... approach to transfer predictors of drug response from pre-clinical ...
单精度,双精度和精度(转载)
Bat common batch script record
mysql基础面试题
机器学习概念梳理(无公式)
组合逻辑块的测试平台
Unity 加密ASE 游戏数据
PID笔记
