当前位置:网站首页>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
边栏推荐
- [Video] Bayesian inference in linear regression and R language prediction of workers' wage data | data sharing
- AI21 Labs | Standing on the Shoulders of Giant Frozen Language Models(站在巨大的冷冻语言模型的肩膀上)
- [tensorflow] sharing mechanism
- Double pointer instrument panel reading (I)
- 普通大学生如何拿到大厂offer?敖丙教你一招致胜!
- The difference between string and character array in C language
- Super 40W bonus pool waiting for you to fight! The second "Changsha bank Cup" Tencent yunqi innovation competition is hot!
- [point cloud series] multi view neural human rendering (NHR)
- 你和42W奖金池,就差一次“长沙银行杯”腾讯云启创新大赛!
- Oracle job scheduled task usage details
猜你喜欢

顶级元宇宙游戏Plato Farm,近期动作不断利好频频

MySQL 8.0.11 download, install and connect tutorials using visualization tools

Logstash数据处理服务的输入插件Input常见类型以及基本使用

Machine learning -- PCA and LDA
![[point cloud series] summary of papers related to implicit expression of point cloud](/img/71/2ea1e8a0d505577c9057670bd06046.png)
[point cloud series] summary of papers related to implicit expression of point cloud

QT调用外部程序

SHA512 / 384 principle and C language implementation (with source code)

Usereducer basic usage

How do ordinary college students get offers from big factories? Ao Bing teaches you one move to win!

@Excellent you! CSDN College Club President Recruitment!
随机推荐
解决tp6下载报错Could not find package topthink/think with stability stable.
Feature Engineering of interview summary
Common analog keys of ADB shell: keycode
JS compares different elements in two arrays
Oracle lock table query and unlocking method
TCP 复位gongji原理和实战复现
[point cloud series] Introduction to scene recognition
Part 3: docker installing MySQL container (custom port)
[quick platoon] 215 The kth largest element in the array
[multi screen interaction] realize dual multi screen display II: startactivity mode
Use of GDB
Oracle index status query and index reconstruction
Example of specific method for TIA to trigger interrupt ob40 based on high-speed counter to realize fixed-point machining action
[point cloud series] neural opportunity point cloud (NOPC)
Servlet of three web components
Interface idempotency problem
AI21 Labs | Standing on the Shoulders of Giant Frozen Language Models(站在巨大的冷冻语言模型的肩膀上)
MySQL 8.0.11 download, install and connect tutorials using visualization tools
ARGB transparency conversion
Longitude and latitude position of provincial capitals in China