当前位置:网站首页>窗口分析函数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
边栏推荐
- Thanos Compact组件测试总结(处理历史数据)
- qs.stringify 接口里把入参转为&连接的字符串(配合application/x-www-form-urlencoded请求头)
- Oracle和mysql批量查询用户下所有表名和表名注释
- Build a cloud blog based on ECS (polite experience)
- 【机器学习】笔记 4、KNN+交叉验证
- Introduction to common APIs for EBFP programming
- Ali vector library Icon tutorial (online, download)
- BCC installation and basic tool instructions
- Thanos Compactor组件使用
- BPF program of type XDP
猜你喜欢

Dolphinscheduler调度sql任务建表时The query did not generate a result set异常解决

PG SQL截取字符串到指定字符位置

基于ECS搭建云上博客(云小宝码上送祝福,免费抽iphone13任务详解)

Introduction to common APIs for EBFP programming

Static interface method calls are not supported at language level ‘5‘ 异常解决

10g数据库使用大内存主机时不能启动的问题

Introduction to RDMA network

Practice of openvswitch VLAN network

Chaos带你快速上手混沌工程

Prometheus cortex Architecture Overview (horizontally scalable, highly available, multi tenant, long-term storage)
随机推荐
冬季实战营 动手实战-MySQL数据库快速部署实践 领鼠标 云小宝
oracle数据库将多个列的查询结果集合并到一行中
Abnormal record-10
Abnormal record-15
19C RAC修改VIP及SCANIP步骤-同网段
switch case时连写常规判断表达式的错误 case和if并用 l
异常记录-12
Practice using polardb and ECs to build portal websites
RAC环境集群组件gipc无法正确识别心跳网络状态问题分析
Oracle redo log产生量大的查找思路与案例
专用窗口函数rank, dense_rank, row_number
异常记录-16
通过rownum来进行区间查询
Dolphinscheduler源码包src.tar.gz解压问题
Winter combat camp hands-on combat - cloud essential environment preparation, hands-on practical operation, quickly build lamp environment, lead mouse cloud Xiaobao backpack without shadow
Thanos Compactor组件使用
Construire un blog Cloud basé sur ECS (bénédiction sur le Code Cloud Xiaobao, explication détaillée de la tâche iphone13 gratuite)
Build an OSS based image sharing website - polite feedback
Use the SED command to process text efficiently
OSS云存储管理实践(体验有礼)