当前位置:网站首页>Window function over
Window function over
2022-07-28 00:11:00 【Meme_ xp】
What are window functions and windows ?
Window function : window 、 function ( Functions applied in the window )— Windows are similar to windows , Limit a space
What is a window ?
The concept of window is very important , It can be understood as a collection of records , Window function is a special function executed on the record set that meets certain conditions . For each record, execute the function... In this window , The window size is fixed , This is a static window ; Different records correspond to different windows , This dynamic window is called sliding window .
structure
function () over()
among ,over Is the key word , Used to specify the window range of function execution , Contains three analysis clauses : grouping (partition by) Clause , Sort (orderby) Clause , window (rows) Clause , If you don't write anything in parentheses , It means that the window contains where All the lines of the condition , The window function calculates based on all rows ; If it's not empty , The following syntax is supported to set the window :
Function name ([expr]) over(partition by < Groups to break down >order by < Columns to sort >rows between< Data range >)
Be careful
rows between 2 preceding and current row# Take the current line and the first two lines
rows between unbounded preceding and current row# Including this line and all previous lines
rows between current row and unbounded fo11owing# Including this line and all subsequent lines
rows between 3 preceding and current row# Including this line and the first three lines
I
rows between 3 preceding and 1 fo7lowing # From the front three lines and the bottom one , Five lines in all
# When order by There is no window clause condition behind , The default window specification is rows between unbounded preceding and currentrow.
# When order by And window clauses are missing , The default window specification is rows between unbounded preceding and unboundedfo11owing
commonly , We can divide window functions into two types :
Proprietary window functions :
rank()
dense_rank()
row_number()·
Aggregate class window function :
sum(),count(),avg(),max(),min()
Example
One 、ROW_NUMBER()
Row_number() The serial number is continuity No repetition , This is true even if you encounter two of the same values in the table
select *,row_number()
OVER(order by number )
as row_num from num

Be careful : In the use of row_number() One thing to pay special attention to when implementing paging ,over In Clause order by To work with SQL Sort... In records order by bring into correspondence with , Otherwise, the sequence number obtained may not be continuous
select *,row_number()
OVER(order by number )
as row_num from num
ORDER BY id

Two 、rank()
Rank() The function will group the same values required to be sorted into a group with the same sequence number of each group , Sorting is not performed consecutively
select *,rank()
OVER(order by number )
as row_num from num

3、 ... and 、dense_rank()
Dense_rank() Sorting is continuous , The same values will also be divided into a group with the same sort number in each group
select *,dense_rank()
OVER(order by number )
as row_num from num

Four 、ntile()
Ntile(group_num) Divide all records into group_num A set of , The serial number of each group is the same
select *,ntile(2)
OVER(order by number )
as row_num from num

边栏推荐
- R语言使用hexSticker包将ggplot2包可视化的结果转换为六角图(六角贴、六角形贴纸、ggplot2 plot to hex sticker)
- 荣耀多款产品齐发,笔记本MagicBook V 14售价6199元起
- UE4 official AEC blueprint case course learning notes
- Flutter pull_ to_ refresh-1.6.0/lib/src/internals/slivers. dart:164:13: Error: Method not found: ‘descr
- 基于mediapipe的姿态识别和简单行为识别
- Realize today's news website based on native JS
- 北欧岗位制博士申请有多难?
- BUUCTF-[BJDCTF2020]RSA1
- How to use C WinForm to copy files and display progress
- Unity implements simple Sketchpad drawing function (notes)
猜你喜欢

4小时定单破20000+,自称“百万内最豪华”,国产品牌飘了?

NDK series (6): let's talk about the way and time to register JNI functions

BUUCTF-RSA roll

基于mediapipe的姿态识别和简单行为识别

Latex中如何加粗字体 & 如何打出圆圈序号

UE4官方AEC蓝图案例课程学习笔记

TCP sticking and unpacking problem + Solution

How Flink uses savepoint
![[December Haikou] the 6th International Conference on ships, marine and Maritime Engineering in 2022 (naome 2022)](/img/a4/041268aadd5d8ff493b52ead9c5e79.png)
[December Haikou] the 6th International Conference on ships, marine and Maritime Engineering in 2022 (naome 2022)

C语言实现五子棋游戏
随机推荐
Sort sort
BUUCTF-RSA roll
为什么需要等待计时2MSL?
传奇服中怎么刷装备
BUU-CTF basic rsa
[NPUCTF2020]EzRSA
XSS Payload 学习浏览器解码
Common errors reported by ant sword
洛谷 P1009 [NOIP1998 普及组] 阶乘之和
2022最新抖音直播监控全套监控(五)商品详情监控
Xss.haozi.me practice customs clearance
给网站套上Cloudflare(以腾讯云为例)
UE4官方AEC蓝图案例课程学习笔记
Design and implementation of spark offline development framework
Bank Marketing预测一个客户购买理财产品的成功率
CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021
TCP的粘包拆包问题+解决方案
新媒体内容输出方式-短视频
资深如何确定软件测试结束的标准
基于原生js实现今日新闻网站