数据分析面试真题 - 开窗函数

18 篇文章 0 订阅

写入数据

create table staffTable( 
department varchar(20) not null, 
name varchar(20),
salary bigint)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
# 插入数据
INSERT INTO staffTable 
(department,name,salary)
VALUES
('HR','jack',8000), 
('HR','peter',7000), 
('HR','merry',6000), 
('BD','harry',10000), 
('BD','lily',9000), 
('BD','Bella',30000), 
('IT','Max',10000), 
('IT','Liz',20000), 
('IT','jason',25000), 
('IT','Raymon',50000);

题目

有department部门,name员工姓名,salary工资三列数据,求出每个部门工资最高的员工,以及每个部门的平均工资

正确🙆

select department, name, salary,
max(salary) over (partition by department) as 'max_salary',
avg(salary) over (partition by department) as 'ave_salary'
from staffTable

在这里插入图片描述

错误🙅

select department, name, salary,
max(salary) over (partition by department order by salary) as 'max_salary',
avg(salary) over (partition by department order by salary) as 'ave_salary'
from staffTable

错误原因:
加了order_by之后,会在分组之内根据排序选取最大值和平均值!!! 注意想要实现的东西,不要瞎排序。
在这里插入图片描述

正确🙆

如果只想取出工资最高的那个人,其他的信息删掉

select department, name, salary, ave_salary from
(select department, name, salary,
-- 排序一定要desc,才能在rank=1时取到工资最高的那个
rank() over (partition by department order by salary desc) as 'rank_salary',
avg(salary) over (partition by department) as 'ave_salary'
from staffTable) ttttable
-- 这个必须写在表外边
where rank_salary = 1

写入数据

create table DAUTable( 
userid varchar(20) not null, 
dt datetime,
score bigint)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
# 插入数据
INSERT INTO DAUTable 
(userid,dt,score)
VALUES
('424',20200101,8000), 
('424',20200102,10000), 
('424',20200104,11000), 
('893ec',20200101,9000), 
('893ec',20200102,8000), 
('893ec',20200103,11000), 
('ccc023dm2',20200103,11000),
('ccc023dm2',20200106,20000);

题目

有userid,score,dt(datetime)三列数据,选出过去10天,有2天评分大于10000的人

普通的group by

select userid,count(dt) as counts from DAUTable
where dt>=20200101 and dt <=20200110
and score >= 10000
group by userid
having counts = 2

小缺点:
user_id对应的score不能被展示出来,除非再进行一次join
在这里插入图片描述

开窗函数 - count()

错误❌1

select userid,score,
count(dt) over (partition by userid order by score) as counts from DAUTable
where dt>=20200101 and dt <=20200110
and score >= 10000

问题!!! 不要加order,不然会排序之后再count
在这里插入图片描述

错误❌2

会先进行where条件的筛选,然后开窗函数的运算;
所以不能在where条件中加counts = 2

select userid,score,
count(dt) over (partition by userid) as counts from DAUTable
where dt>=20200101 and dt <=20200110
and score >= 10000

在这里插入图片描述

错误❌3

select userid,score,
count(dt) over (partition by userid) as counts from DAUTable
where dt>=20200101 and dt <=20200110
and score >= 10000
and count(dt) over (partition by userid) = 2

正确

select userid,score from
(select userid,score,
count(dt) over (partition by userid) as counts from DAUTable
where dt>=20200101 and dt <=20200110
and score >= 10000) ttt
where counts = 2

在这里插入图片描述

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值