SQL开窗函数之percent_rank、first_value、nth的用法
创始人
2024-04-21 14:03:57
0

开窗函数

当我们需要进行一些比较复杂的子查询时,聚合函数就会非常的麻烦,因此可以使用开窗函数进行分组再运用函数查询。窗口函数既可以显示聚集前的数据,也可以显示聚集后的数据,可以在同一行中返回基础行的列值和聚合后的结果列

常见运用场景: 对班里同学成绩进行排序

常见的窗口函数
在这里插入图片描述

开窗函数基本形式

func_name() 
OVER(
[PARTITION BY ] 
[ORDER BY  ASC|DESC]
[rows between ?? And ??]
)

具体字段的解释看我的上一篇:SQL开窗函数之基本用法和聚合函数


分布函数

percent_rank(): 按公式(rank-1)/(row-1)进行计算
应用场景:不常用

cume_dist(): 获取组内小于等于当前rank值的行数/分组内总行数
应用场景:查询小于当前薪资的比例

e.g. Sales表
在这里插入图片描述

select *, 
rank() over(order by sales) as ranking,
percent_rank() over(order by sales) as percent_ranking,
cume_dist() over(order by sales) as cume 
from Sales

在这里插入图片描述


头尾函数

头尾函数 first_value()last_value() 主要用于获取分组字段内的第一个值或最后一个值,部分情况下相当于 maxmin
应用场景: 查询部门最早发生销售记录日期和最近发生的销售记录日期

e.g. 成绩表
在这里插入图片描述

select *,
first_value(score) over(partition by cid),
first_value(score) over(partition by cid order by score),
last_value(score) over(partition by cid), 
last_value(score) over(partition by cid order by score) 
from sc;

在这里插入图片描述

Ntile函数

NTILE()函数用于将分区中的有序数据分为n个等级,记录等级数

NTILE(n) 
OVER (PARTITION BY [{,...}]ORDER BY  [ASC|DESC], [{,...}]
) 

Ntile(n)表示分成了n组

e.g. 成绩表
在这里插入图片描述

  • 把学生成绩分成2组和3组
select *,
ntile(2) over(order by score desc) as 2_tile,
ntile(3) over(order by score desc) as 3_tile,
from sc;

在这里插入图片描述

  • 把成绩按cid分成2组和3组
select *,
ntile(2) over(partition by cid order by score desc) as 2_tile_group,
ntile(3) over(partition by cid order by score desc) as 3_tile_group
from sc;

在这里插入图片描述

应用:选取薪水前50%的员工
Employee表
在这里插入图片描述

思路:

  1. 先给员工薪水分成2组进行排序
  2. 筛选薪水前50%的员工的序号
-- 先给员工薪水分成2组
select *, 
ntile(2) over(order by salary) as ranks 
from employees

在这里插入图片描述

-- 筛选薪水前50%的员工的序号
select * from (select *, ntile(2) over(order by salary) as ranks from employees
) t
where ranks = 2

在这里插入图片描述


nth_value函数

nth_value()函数用于返回分组内到当前行的第N行的值。如果第N行不存在,则函数返回NULL。
N必须是正整数,例如1,2和3。

应用场景: 查询第N名的同学信息
基本形式:

NTH_VALUE(expression, N)
OVER ([PARTITION BY ] [ORDER BY  ASC|DESC][rows between ?? And ??]
)

NTH_VALUE(expression, N) 表示在 expression 里第 N

e.g. 成绩表
在这里插入图片描述

select *,
nth_value(score, 1) over(partition by cid order by score desc) as 1th,
nth_value(score, 2) over(partition by cid order by score desc) as 2th,
nth_value(score, 3) over(partition by cid order by score desc) as 3th
from sc;

在这里插入图片描述

select *,
nth_value(score, 1) over(partition by cid) as 1th,
nth_value(score, 2) over(partition by cid) as 2th,
nth_value(score, 3) over(partition by cid) as 3th
from sc;

在这里插入图片描述

应用:获取各班分数第一名的同学id

在这里插入图片描述

思路:

  1. 先给个班的分数排序,获取第一名的分数
  2. 根据分数获取第一名同学的信息
-- 先获取每个班第一名的分数
select *, 
nth_value(score,1) over(partition by cid order by score desc) as 1th_score 
from sc;

在这里插入图片描述

-- 根据分数获得同学信息
select sid, cid, score from (select *, nth_value(score,1) over(partition by cid order by score desc) as 1th_score from sc) t
where score=1th_score

在这里插入图片描述


参考来源:
MySQL模块:开窗函数
SQL中开窗函数first_value() 和 last_value()
MySQL8中的开窗函数

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...