当前位置:网站首页>窗口分析函数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
边栏推荐
猜你喜欢

Apache SeaTunnel 2.1.0部署及踩坑

Memcached source code analysis

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

BPF program of type XDP

Chaos vous emmène au projet chaos.

Practice of openvswitch VLAN network

实践使用PolarDB和ECS搭建门户网站

Typical application scenarios of alicloud log service SLS

High performance gateway for interconnection between VPC and IDC based on dpdk

Thanos compact component test summary (processing historical data)
随机推荐
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
Prometheus cortex Architecture Overview (horizontally scalable, highly available, multi tenant, long-term storage)
Alertmanager重复/缺失告警现象探究及两个关键参数group_wait和group_interval的释义
解决:You have 18 unapplied migration(s). Your project may not work properly until you apply
Virtio and Vhost_ Net introduction
When switch case, concatenate the error case and if of the conventional judgment expression and use L
基于ECS搭建云上博客(体验有礼)
Implementation of multi tenant read and write in Prometheus cortex
Dolphinscheduler源码包src.tar.gz解压问题
Abnormal record-21
异常记录-16
Will restarting the Oracle listener break the existing connection
基于EMR离线数据分析-反馈有礼
一个DG环境的ORA-16047: DGID mismatch between destination setting and target database问题排查及监听VNCR特性
Oracle net service: listener and service name resolution method
19C环境ORA-01035登陆报错处理
Memcached source code analysis
RAC环境集群组件gpnp未启动成功问题分析
Chaos带你快速上手混沌工程
mysql和pgsql时间相关操作