当前位置:网站首页>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

边栏推荐
- 2022/7/26
- 数据中台的那些“经验与陷阱”
- Flutter pull_ to_ refresh-1.6.0/lib/src/internals/slivers. dart:164:13: Error: Method not found: ‘descr
- ESP8266-----MQTT云下设备上云
- Shell programming specifications and variables
- 29. Learn the stacked column chart of highcharts using percentage
- BUUCTF-Baby RSA
- Xss.haozi.me practice customs clearance
- Use a grayscale filter
- Posture recognition and simple behavior recognition based on mediapipe
猜你喜欢

Realize today's news website based on native JS

抖音直播监控-循环值守24小时-直播弹幕

传奇外网架设教程带图文解说——Gom引擎

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

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

Monologue of a software Investor: why don't I pursue fast-growing companies

Xss.haozi.me practice customs clearance

J9数字科普:Sui网络的双共识是如何工作的?

给网站套上Cloudflare(以腾讯云为例)

require、loadfile、dofile、load、loadstring
随机推荐
BUUCTF-childRSA费马小定理
虚拟存储器与Cache的比较
「图神经网络:基础、前沿与应用」最新IJCAI2022教程
Latex中如何加粗字体 & 如何打出圆圈序号
面试官问线程安全的List,看完再也不怕了!
[December Haikou] the 6th International Conference on ships, marine and Maritime Engineering in 2022 (naome 2022)
Why do I need to wait for 2msl?
New technology leads new changes in marketing of large and medium-sized enterprises, and UFIDA BiP CRM is launched!
How to deal with the website after it is hacked and how to delete batch malicious code
The 4-hour order exceeds 20000+, claiming to be "the most luxurious in a million". Is the domestic brand floating?
Latex common summary (2): input matrix (input matrix, diagonal matrix, equations, etc.)
TCP sticking and unpacking problem + Solution
传奇服中怎么刷装备
北欧岗位制博士申请有多难?
[RoarCTF2019]RSA
XSS payload learning browser decoding
BUUCTF-RSA roll
Latex常用总结(2):输入矩阵(输入矩阵、对角阵、方程组等)
正值三伏天我却被吓出了冷汗:驾驶安全隐患离我们有多远?
MQTT----mqtt.fx客户端软件