当前位置:网站首页>窗口分析函数LAST_VALUE,FIRST_VALUE,lag,lead
窗口分析函数LAST_VALUE,FIRST_VALUE,lag,lead
2022-04-23 06:04:00 【旺财2】
目录
FIRST_VALUE和LAST_VALUE详解
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 一看就明白了。但last_value 为什么就是当前行的值呢?明明该是4才对啊。
原因在于这两个函数 可以用rows 指定作用域。 而默认的作用域是
RANGE UNBOUNDED PRECEDING AND CURRENT ROW
就是说从窗口的第一行到当前行。 所以last_value 最后一行肯定是当前行了。
知道原因后,只需要改掉行的作用域就可以了。
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 子句的一些关键字:
UNBOUNDED PRECEDING 窗口函数第一行
UNBOUNDED following 窗口函数最后一行
CURRENT ROW 当前行
n PRECEDING 当前行的前几行
n following 当前行的后几行
lag和lead详解
LAG(col,n,DEFAULT) 用于统计窗口内往上第n行值
- 第一个参数为列名,
- 第二个参数为往上第n行(可选,默认为1),
- 第三个参数为默认值(当往上第n行为NULL时候,取默认值,如不指定,则为NULL)
select field1,field2,LAG(field2,1) over (partition by field1 order by field4) as next_field2,field4
from test1
LEAD(col,n,DEFAULT) 用于统计窗口内往下第n行值
- 第一个参数为列名,
- 第二个参数为往下第n行(可选,默认为1),
- 第三个参数为默认值(当往下第n行为NULL时候,取默认值,如不指定,则为NULL)
select field1,field2,LEAD(field2,1) over (partition by field1 order by field4) as next_field2,field4
from test1
版权声明
本文为[旺财2]所创,转载请带上原文链接,感谢
https://blog.csdn.net/wdyliuxingfeiyang/article/details/122230234
边栏推荐
- EMR Based offline data analysis - polite feedback
- 阿里矢量库的图标使用教程(在线,下载)
- Memcached source code analysis
- Oracle RAC数据库实例启动异常问题分析IPC Send timeout
- When switch case, concatenate the error case and if of the conventional judgment expression and use L
- Oracle net service: listener and service name resolution method
- pg库查看某模式下某表的分布键
- Ali vector library Icon tutorial (online, download)
- Abnormal record-17
- oracle生成毫秒级时间戳
猜你喜欢
基于ECS搭建云上博客(云小宝码上送祝福,免费抽iphone13任务详解)
ACFS文件系统创建扩大缩小等配置步骤
10g数据库使用大内存主机时不能启动的问题
通过源码探究@ModelAndView如何实现数据与页面的转发
阿里云日志服务sls的典型应用场景
Implementation of multi tenant read and write in Prometheus cortex
Build a cloud blog based on ECS (send blessings on the cloud Xiaobao code and draw iphone13 for free)
使用prom-label-proxy实现Prometheus Thanos的基于标签的多租户读
Try catch cannot catch asynchronous errors
Introduction to DDoS attack / defense
随机推荐
Abnormal record-18
Analysis of Rdam principle
oracle库恢复数据
Oracle net service: listener and service name resolution method
冬季实战营 动手实战-MySQL数据库快速部署实践 领鼠标 云小宝
Abnormal record-13
Abnormal record-11
JS format current time and date calculation
19C RAC修改VIP及SCANIP步骤-同网段
Abnormal record-15
Abnormal record-16
OSS云存储管理实践(体验有礼)
Oracle RAC数据库实例启动异常问题分析IPC Send timeout
mysql和pg库遇到冲突数据时的两种处理方式
Dolphinscheduler源码包src.tar.gz解压问题
Exception record-7
冬季实战营动手实战-上云必备环境准备,动手实操快速搭建LAMP环境 领鼠标 云小宝 背包 无影
异常记录-13
Prometheus Cortex使用Block存储时的相关问题
异常记录-15