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

边栏推荐
- J9 Digital Science Popularization: how does the double consensus of Sui network work?
- Legendary server: what must be modified when the GOM geem2 engine is updated?
- MapReduce (III)
- 为什么 Redis 集群要使用反向代理? 看这篇就明白了
- BUUCTF-childRSA费马小定理
- [December Haikou] the 6th International Conference on ships, marine and Maritime Engineering in 2022 (naome 2022)
- If we were the developer responsible for repairing the collapse of station B that night
- Put cloudflare on the website (take Tencent cloud as an example)
- 给网站套上Cloudflare(以腾讯云为例)
- Glory launched a number of products at the same time. The price of notebook magicbook V 14 starts from 6199 yuan
猜你喜欢

Common errors reported by ant sword
![[flight control development foundation tutorial 6] crazy shell · open source formation UAV SPI (six axis sensor data acquisition)](/img/75/509db67a580dd4b9849bea08845cc7.png)
[flight control development foundation tutorial 6] crazy shell · open source formation UAV SPI (six axis sensor data acquisition)

C语言实现五子棋游戏

北欧岗位制博士申请有多难?

Why does redis cluster use reverse proxy? Just read this one

Latex常用总结(2):输入矩阵(输入矩阵、对角阵、方程组等)

(十二)51单片机----用DS18B20浅测一下工(江)西的室外温度

荣耀多款产品齐发,笔记本MagicBook V 14售价6199元起

The 4-hour order exceeds 20000+, claiming to be "the most luxurious in a million". Is the domestic brand floating?

Bank Marketing预测一个客户购买理财产品的成功率
随机推荐
五子棋人机对战实现
BUUCTF-bbbbbbrsa
Control mode of CPU
学yolo需要什么基础?怎么学YOLO?
How to bold font in Latex & how to make circle serial number
C语言实现五子棋游戏
[NCTF2019]babyRSA1
J9数字科普:Sui网络的双共识是如何工作的?
BUU-CTF basic rsa
UE4 official AEC blueprint case course learning notes
JS ATM机输出
XSS payload learning browser decoding
【21天学习挑战赛】K同学啊 邀你参加深度学习研讨班
Shuffle, partition and read of tfrecord
[NPUCTF2020]EzRSA
Oracle密码过期解决办法
[MRCTF2020]babyRSA
Reduce error demonstration
基于原生js实现今日新闻网站
BUU-CTF basic rsa