当前位置:网站首页>窗口函数over
窗口函数over
2022-07-27 21:16:00 【Meme_xp】
什么是窗口函数和窗口?
窗口函数:窗口、函数(应用在窗口内的函数)—窗口类似于窗户,限定一个空间范围
那什么叫窗口呢?
窗口的概念非常重要,它可以理解为记录集合,窗口函数也就是在满足某种条件的记录集合上执行的特殊函数。对于每条记录都要在此窗口内执行函数,窗口大小都是固定的,这种属于静态窗口;不同的记录对应着不同的窗口,这种动态变化的窗口叫滑动窗口。
结构
函数() over()
其中,over是关键字,用来指定函数执行的窗口范围,包含三个分析子句:分组(partition by)子句,排序(orderby)子句,窗口(rows)子句,如果后面括号中什么都不写,则意味着窗口包含满足where条件的所有行,窗口函数基于所有行进行计算;如果不为空,则支持以下语法来设置窗口:
函数名([expr]) over(partition by <要分列的组>order by <要排序的列>rows between<数据范围>)
注意
rows between 2 preceding and current row#取当前行和前面两行
rows between unbounded preceding and current row#包括本行和之前所有的行
rows between current row and unbounded fo11owing#包括本行和之后所有的行
rows between 3 preceding and current row#包括本行和前面三行
I
rows between 3 preceding and 1 fo7lowing #从前面三行和下面一行,总共五行
#当order by后面缺少窗口从句条件,窗口规范默认是rows between unbounded preceding and currentrow.
#当order by和窗口从句都缺失,窗口规范默认是 rows between unbounded preceding and unboundedfo11owing
一般,我们可以把窗口函数分为两种:
专有窗口函数:
rank()
dense_rank()
row_number()·
聚合类窗口函数:
sum(),count(),avg(),max(),min()
例子
一、ROW_NUMBER()
Row_number() 在排名是序号 连续 不重复,即使遇到表中的两个一样的数值亦是如此
select *,row_number()
OVER(order by number )
as row_num from num

注意:在使用row_number() 实现分页时需要特别注意一点,over子句中的order by 要与SQL排序记录中的order by保持一致,否则得到的序号可能不是连续的
select *,row_number()
OVER(order by number )
as row_num from num
ORDER BY id

二、rank()
Rank() 函数会把要求排序的值相同的归为一组且每组序号一样,排序不会连续执行
select *,rank()
OVER(order by number )
as row_num from num

三、dense_rank()
Dense_rank() 排序是连续的,也会把相同的值分为一组且每组排序号一样
select *,dense_rank()
OVER(order by number )
as row_num from num

四、ntile()
Ntile(group_num) 将所有记录分成group_num个组,每组序号一样
select *,ntile(2)
OVER(order by number )
as row_num from num

边栏推荐
- 给网站套上Cloudflare(以腾讯云为例)
- [Development Tutorial 9] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - heart rate monitoring
- BUUCTF-childRSA费马小定理
- BUU-CTF basic rsa
- [ACTF新生赛2020]crypto-aes
- The share price soared 180.46%! Shanghai silicon industry, the leader of domestic large silicon wafers, is listed: the cumulative net profit in recent four years is less than 60million
- Realization of gobang man-machine combat
- Senior how to determine the standard of software test completion
- MapReduce (III)
- UE4 official AEC blueprint case course learning notes
猜你喜欢

How Flink uses savepoint

Redefine analysis - release of eventbridge real-time event analysis platform
![[Development Tutorial 9] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - heart rate monitoring](/img/cc/91ec9f2c2cd5d5bddd157e61191772.png)
[Development Tutorial 9] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - heart rate monitoring
![[GWCTF 2019]BabyRSA1](/img/31/6727fd04be13ddd6bd46969fe2c50f.png)
[GWCTF 2019]BabyRSA1

liux常用命令(查看及其开放防火墙端口号+查看及其杀死进程)

BUUCTF-RSA4

Put cloudflare on the website (take Tencent cloud as an example)

(12) 51 Single Chip Microcomputer -- use DS18B20 to measure the outdoor temperature in Gongjiang West

J9 Digital Science Popularization: how does the double consensus of Sui network work?

MQTT----mqtt.fx客户端软件
随机推荐
Those "experiences and traps" in the data center
Zcmu--1720: death is like the wind, I want to pretend to force
TOGAF10标准读书会首场活动圆满举办,精彩时刻回顾!
Senior how to determine the standard of software test completion
[MRCTF2020]babyRSA
Starfish OS X metabell strategic cooperation, metauniverse business ecosystem further
Error:svn: E155010: ‘/Users/.../Desktop/wrokspace/xxx‘ is scheduled for addition, but is missing
The first activity of togaf10 standard reading club was successfully held, and the wonderful moments were reviewed!
资深如何确定软件测试结束的标准
Explain the idempotence of distributed system in detail
Arm32 for remote debugging
传奇外网架设教程带图文解说——Gom引擎
Latex中如何加粗字体 & 如何打出圆圈序号
基于mediapipe的姿态识别和简单行为识别
Arm32进行远程调试
Binary conversion method
[MRCTF2020]babyRSA
Common errors reported by ant sword
尚硅谷尚品项目汇笔记(一)
2022/7/26