当前位置: 首页 > news >正文

开窗函数 - first_value/last_value


1、开窗函数是什么?

开窗函数用于为行定义一个窗口(这里的窗口是指运算将要操作的行的集合),它对一组值进行操作,不需要使用 GROUP BY 子句对数据进行分组,能够在同一行中同时返回基础行的列和聚合列。

2、开窗函数有什么用?

开窗函数的功能本质是聚合,但是相比聚合,开窗函数可以提供的信息更多。

3、first_value/last_value 函数

	first_value()over(partition by 列名1,列名2 order by 列名1,列名2)是求一组数据的第一个值last_value()over(partition by 列名1,列名2 order by 列名1,列名2)是求一组数据的最后一个值

first_value 用法:

	select distinct a.date,a.name,first_value(date)over(partition by name order by date asc)as `每个人对应最早的date`,first_value(date)over(partition by name order by date desc)as `每个人对应最晚的date`from (select '张三'as name,'2021-04-11' as date union all select '李四'as name,'2021-04-09' as date union all select '赵四'as name,'2021-04-16' as date union all select '张三'as name,'2021-03-10'as dateunion all select '李四'as name,'2020-01-01'as date)a 

last_value 用法

	select distinct a.date,a.name,last_value(date)over(partition by name order by date asc)as `每个人对应最晚的date`from (select '张三'as name,'2021-04-11' as date union all select '李四'as name,'2021-04-09' as date union all select '赵四'as name,'2021-04-16' as date union all select '张三'as name,'2021-03-10'as dateunion all select '李四'as name,'2020-01-01'as date)a 


可以看到使用 last_value 函数求每个人最后一个日期,结果并不是想要的。那该怎么办呢,查询该函数的具体用法发现:

last_value() 默认的统计范围是”rows between unbounded preceding and current row【无界的前面行和当前行之间】” 怎么理解呢?见下:

	rows between unbounded preceding and current row,可以这么理解: x∈(-∞,X)rows between unbounded preceding and unbounded following,    x∈(-∞,+ ∞)rows between current row and unbounded following,            x∈(X,+ ∞) 

last_value() 默认是升序,如果限制了是降序,则等同于 first_value() 升序

	select distinct a.date,a.name,last_value(date)over(partition by name order by date rows between unbounded preceding and current row)as `(-∞,X)`,last_value(date)over(partition by name order by date rows between unbounded preceding and unbounded following)as `(-∞,+ ∞)`,last_value(date)over(partition by name order by date rows between current row and unbounded following)as `(X,+ ∞)`from (select '张三'as name,'2021-04-11' as date union all select '李四'as name,'2021-04-09' as date union all select '赵四'as name,'2021-04-16' as date union all select '张三'as name,'2021-03-10'as dateunion all select '李四'as name,'2020-01-01'as date)a 

http://www.lryc.cn/news/488257.html

相关文章:

  • 「一」HarmonyOS端云一体化概要
  • nodejs21: 快速构建自定义设计样式Tailwind CSS
  • 从JSON数据提取嵌套字段并转换为独立列的简洁方法
  • 湘潭大学软件工程算法设计与分析考试复习笔记(四)
  • 特征交叉-DeepCross Network学习
  • stm32cubemx+VSCODE+GCC+makefile 开发环境搭建
  • Go语言中的Defer机制详解与示例
  • H.265流媒体播放器EasyPlayer.js H5流媒体播放器如何验证视频播放是否走硬解
  • ms-hot目录
  • vulfocus在线靶场:骑士cms_cve_2020_35339:latest 速通手册
  • AI Large Language Model
  • React Native的`react-native-reanimated`库中的`useAnimatedStyle`钩子来创建一个动画样式
  • FastJson反序列化漏洞(CVE-2017-18349)
  • 【优选算法篇】分治乾坤,万物归一:在重组中窥见无声的秩序
  • C++:探索AVL树旋转的奥秘
  • 2. Django中的URL调度器 (自定义路径转换器)
  • 深度学习:神经网络中线性层的使用
  • 【刷题】算法设计题+程序设计题【2】2019-2024
  • 搭建es环境
  • 阿里云和七牛云对象存储区别和实现
  • uniapp微信小程序接入airkiss插件进行WIFI配网
  • 03 —— Webpack 自动生成 html 文件
  • Python毕业设计选题:基于python的豆瓣电影数据分析可视化系统-flask+spider
  • 抽象类能使用final修饰吗?
  • C语言内存:我家大门常打开
  • 路由协议——iBGP与EBGP
  • 【Linux】基础02
  • Elasticsearch面试内容整理-安全与权限管理
  • 【数据分享】中国汽车工业年鉴(1986-2023)
  • el-cascader 使用笔记