当前位置:网站首页>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
边栏推荐
- Tersus notes employee information 516 MySQL query (time period uniqueness judgment of 2 fields)
- 叮~ 你的奖学金已到账!C认证企业奖学金名单出炉
- Xi'an CSDN signed a contract with Xi'an Siyuan University, opening a new chapter in IT talent training
- Oracle lock table query and unlocking method
- Lpddr4 notes
- Database transactions
- According to the salary statistics of programmers in June 2021, the average salary is 15052 yuan. Are you holding back?
- 集简云 x 飞书深诺,助力企业运营部实现自动化办公
- Esp32 vhci architecture sets scan mode for traditional Bluetooth, so that the device can be searched
- SHA512 / 384 principle and C language implementation (with source code)
猜你喜欢
Unified task distribution scheduling execution framework
SAP ui5 application development tutorial 72 - animation effect setting of SAP ui5 page routing
Set Jianyun x Feishu Shennuo to help the enterprise operation Department realize office automation
The difference between string and character array in C language
The interviewer dug a hole for me: how many concurrent TCP connections can a single server have?
面试官给我挖坑:单台服务器并发TCP连接数到底可以有多少 ?
超40W奖金池等你来战!第二届“长沙银行杯”腾讯云启创新大赛火热来袭!
Xi'an CSDN signed a contract with Xi'an Siyuan University, opening a new chapter in IT talent training
Example of specific method for TIA to trigger interrupt ob40 based on high-speed counter to realize fixed-point machining action
LeetCode_ DFS_ Medium_ 695. Maximum area of the island
随机推荐
Innobackupex incremental backup
Common types and basic usage of input plug-in of logstash data processing service
[point cloud series] multi view neural human rendering (NHR)
9419 page analysis of the latest first-line Internet Android interview questions
面试官给我挖坑:URI中的 “//” 有什么用?
Xi'an CSDN signed a contract with Xi'an Siyuan University, opening a new chapter in IT talent training
Error 403 in most cases, you or one of your dependencies are requesting
What does the SQL name mean
@Excellent you! CSDN College Club President Recruitment!
Solve the problem that Oracle needs to set IP every time in the virtual machine
Machine learning -- model optimization
Filter and listener of three web components
校园外卖系统 - 「农职邦」微信原生云开发小程序
Tangent space
Oracle kills the executing SQL
Oracle lock table query and unlocking method
Playwright contrôle l'ouverture de la navigation Google locale et télécharge des fichiers
According to the salary statistics of programmers in June 2021, the average salary is 15052 yuan. Are you holding back?
Detailed explanation of constraints of Oracle table
[barycentric coordinate interpolation, perspective correction interpolation] principle and usage opinions