当前位置:网站首页>Window analysis function last_ VALUE,FIRST_ VALUE,lag,lead
Window analysis function last_ VALUE,FIRST_ VALUE,lag,lead
2022-04-23 13:41:00 【Wangcai 2】
Catalog
FIRST_VALUE and LAST_VALUE Detailed explanation
lag and lead Detailed explanation
FIRST_VALUE and LAST_VALUE Detailed explanation
with abc as
(
select 1 as id union all
select 2 union all
select 3 union all
select 4
)
select id,
FIRST_VALUE(id) over(order by id ) as firstid,
LAST_VALUE(id) over(order by id) as lastid
from abc

FIRST_VALUE It's easy to see . but last_value Why is it the value of the current row ? It should be 4 That's right .
The reason is that these two functions It can be used rows Specify scope . The default scope is
RANGE UNBOUNDED PRECEDING AND CURRENT ROW
That is, from the first line of the window to the current line . therefore last_value The last line must be the current line .
When you know why , Just change the scope of the line .
with abc as
(
select 1 as id union all
select 2 union all
select 3 uno union all
select 4
)
select id,
FIRST_VALUE(id) over(order by id ) as firstid,
LAST_VALUE(id) over(order by id rows between UNBOUNDED PRECEDING AND UNBOUNDED following ) as lastid
from abc

rows Some keywords of clause :
UNBOUNDED PRECEDING The first line of the window function
UNBOUNDED following The last line of the window function
CURRENT ROW Current row
n PRECEDING The first few lines of the current line
n following The last few lines of the current line
lag and lead Detailed explanation
LAG(col,n,DEFAULT) It is used to count up the n Row value
- The first parameter is the column name ,
- The second parameter is upward n That's ok ( Optional , The default is 1),
- The third parameter is the default value ( When you go up n Behavior NULL When , Take the default value , If not specified , Then for NULL)
select field1,field2,LAG(field2,1) over (partition by field1 order by field4) as next_field2,field4
from test1

LEAD(col,n,DEFAULT) Used for the next... In the statistics window n Row value
- The first parameter is the column name ,
- The second parameter is next n That's ok ( Optional , The default is 1),
- The third parameter is the default value ( When you go down n Behavior NULL When , Take the default value , If not specified , Then for NULL)
select field1,field2,LEAD(field2,1) over (partition by field1 order by field4) as next_field2,field4
from test1

版权声明
本文为[Wangcai 2]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230604300588.html
边栏推荐
- 9419 page analysis of the latest first-line Internet Android interview questions
- CSDN College Club "famous teacher college trip" -- Hunan Normal University Station
- Interface idempotency problem
- [point cloud series] deepmapping: unsupervised map estimation from multiple point clouds
- 十万大学生都已成为猿粉,你还在等什么?
- Software test system integration project management engineer full truth simulation question (including answer and analysis)
- [multi screen interaction] realize dual multi screen display II: startactivity mode
- Example interview | sun Guanghao: College Club grows and starts a business with me
- Oracle defines self incrementing primary keys through triggers and sequences, and sets a scheduled task to insert a piece of data into the target table every second
- JS compares different elements in two arrays
猜你喜欢

Ding ~ your scholarship has arrived! C certified enterprise scholarship list released

十万大学生都已成为猿粉,你还在等什么?

Esp32 vhci architecture sets scan mode for traditional Bluetooth, so that the device can be searched

Loading and using image classification dataset fashion MNIST in pytorch

LeetCode_ DFS_ Medium_ 695. Maximum area of the island

AI21 Labs | Standing on the Shoulders of Giant Frozen Language Models(站在巨大的冷冻语言模型的肩膀上)

CSDN College Club "famous teacher college trip" -- Hunan Normal University Station

Set Jianyun x Feishu Shennuo to help the enterprise operation Department realize office automation

Analysis of the latest Android high frequency interview questions in 2020 (BAT TMD JD Xiaomi)

集简云 x 飞书深诺,助力企业运营部实现自动化办公
随机推荐
SAP ui5 application development tutorial 72 - trial version of animation effect setting of SAP ui5 page routing
Oracle index status query and index reconstruction
Loading and using image classification dataset fashion MNIST in pytorch
Machine learning -- PCA and LDA
Resolution: argument 'radius' is required to be an integer
“湘见”技术沙龙 | 程序员&CSDN的进阶之路
[dynamic programming] 221 Largest Square
Utilisation de GDB
Ding ~ your scholarship has arrived! C certified enterprise scholarship list released
playwright控制本地谷歌浏览打开,并下载文件
[barycentric coordinate interpolation, perspective correction interpolation] principle and usage opinions
[point cloud series] multi view neural human rendering (NHR)
Interval query through rownum
GDB的使用
Servlet of three web components
[indicators] precision, recall
[point cloud series] pointfilter: point cloud filtering via encoder decoder modeling
[point cloud series] relationship based point cloud completion
[point cloud series] Introduction to scene recognition
[tensorflow] sharing mechanism