PostgreSQL 窗口函数

news2024/11/15 18:38:43

随着数据库的发展迭代,有一些被人们常用的功能已经在数据库中被做成窗口函数来实现了,窗口函数的引入带来了极大的方便。下面对几个常见的实现做下简单的介绍。

PostgreSQL 数据库通用的窗口函数
函数            描述
row_number      当前行在其分区中的行号,从 1 开始计
rank            带间隙的当前行排名。与该行的第一个同等行的 row_number 相同
dense_rank      不带间隙的当前行排名
percent_rank    当前行的相对排名 rank-1/总行数 - 1
first_value     返回在窗口帧中第一行上计算的 VALUE
last_value      返回在窗口帧中最后一行上计算的 VALUE
nth_value       返回在窗口帧中第 N 行上计算的 VALUE ,没有这样的行则返回空值

构造和模拟测试数据
CREATE TABLE "public"."windows_functions" (
  "vaf01" int8 NOT NULL,
  "bbx01" int4 NOT NULL,
  "vaa01" int8 NOT NULL,
  "vaf06" int8,
  "vaf21" numeric(18,4),
  "vaf36" timestamp(6),
  "bce02a" varchar(20),
  PRIMARY KEY ("vaf01"));

ALTER TABLE "public"."windows_functions" OWNER TO "postgres";

INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (658728616326209540, 135, 658239514644455424, 658239514644455425, '1.0000', '2021-12-23 17:52:02', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (658731204983529490, 135, 658239514644455424, 658239514644455425, '1.0000', '2021-12-23 17:57:59', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (658731645389643794, 135, 658239514644455424, 658239514644455425, '1.0000', '2021-12-23 18:04:16', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (551054176646471682, 141, 551053257468944384, 551053257468944385, '1.0000', '2021-03-01 14:52:21', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (551701005100785664, 141, 551698907898462208, 551698908091400192, '1.0000', '2021-03-03 09:42:23', '2909');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (627165126042198016, 141, 538649333814403072, 627162666070319105, '1.0000', '2021-09-27 15:29:23', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (570926872293154816, 144, 552536561183506432, 570925895129374721, '1.0000', '2021-04-25 10:59:29', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (658728616326209536, 144, 658239514644455424, 658239514644455425, '1.0000', '2021-12-23 17:52:02', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (658731204983529486, 144, 658239514644455424, 658239514644455425, '1.0000', '2021-12-23 17:57:59', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (658731645389643790, 144, 658239514644455424, 658239514644455425, '1.0000', '2021-12-23 18:04:16', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (535825623831158785, 158, 535762576789880832, 535762576844406784, '1.0000', '2021-01-18 14:18:47', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (535825623831158786, 158, 535762576789880832, 535762576844406784, '1.0000', '2021-01-18 14:18:47', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (535825623831158787, 158, 535762576789880832, 535762576844406784, '1.0000', '2021-01-18 14:18:47', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (535825623831158788, 158, 535762576789880832, 535762576844406784, '1.0000', '2021-01-18 14:18:47', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (726737386096173058, 158, 549156263519461376, 726737120902914049, '1.0000', '2022-06-29 09:54:29', '1327');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (724594107497979906, 161, 724593621424283648, 724593621424283649, '1.0000', '2022-06-23 11:58:17', '1417');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (729302667771387909, 161, 729300393015779328, 729300393015779329, '1.0000', '2022-07-06 11:48:24', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (863466615524708353, 161, 615189645499703296, 863461644267835393, '1.0000', '2023-07-11 17:08:02', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (516210446089334785, 165, 516195929204858880, 516195929204858881, '1.0000', '2020-11-25 11:15:59', '1317');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (516213731345051649, 165, 516195929204858880, 516195929204858881, '1.0000', '2020-11-25 11:29:03', '1317');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (569892501071208449, 165, 569887714174967808, 569887714439208960, '1.0000', '2021-04-22 14:29:18', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (727567424773300225, 165, 722409122246565888, 727488339111976961, '1.0000', '2022-07-01 16:53:11', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (881202964285509632, 165, 535843758638514177, 880871334060642305, '1.0000', '2023-08-29 15:45:40', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (513818311230763009, 168, 507967861428469760, 513721626081247232, '1.0000', '2020-11-18 20:50:18', '1317');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (531507780406878209, 168, 531505335798743040, 531505336008458240, '1.0000', '2021-01-06 16:22:09', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (805118984532926464, 168, 805004845214998528, 805004846649450496, '3.0000', '2023-01-31 16:55:05', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (805118984532926465, 168, 805004845214998528, 805004846649450496, '3.0000', '2023-01-31 16:55:05', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (514028077471248408, 181, 507967861428469760, 513721626081247232, '1.0000', '2020-11-19 10:43:24', '1317');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (518893461668241417, 181, 518456253970784256, 518456253970784257, '1.0000', '2020-12-02 20:57:07', '1317');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (612680959363522561, 181, 612668560447250432, 612668807080714241, '1.0000', '2021-08-18 16:15:23', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (743773524774232065, 181, 743117897265717248, 743752713933824001, '1.0000', '2022-08-15 10:10:25', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (870970629162160128, 181, 552452918809337856, 870958825786724353, '1.0000', '2023-08-01 10:06:17', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (535821302968229911, 182, 535753930509008896, 535753930626449408, '1.0000', '2021-01-18 14:02:33', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (628272603169169408, 182, 628271142662184960, 628271142662184961, '1.0000', '2021-09-30 16:48:19', '1417');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (738759730964602883, 182, 738415678138228736, 738415678138228737, '1.0000', '2022-08-01 14:07:23', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (809714627037503489, 182, 809710848791683072, 809710850574262272, '1.0000', '2023-02-13 09:16:50', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (732183536827768833, 189, 732162271870066688, 732162271966535680, '1.0000', '2022-07-14 10:35:58', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (734773025429397505, 189, 734771344595623936, 734771344750813184, '1.0000', '2022-07-21 14:05:40', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (755842628033060877, 189, 523788638480375808, 755806701562765313, '1.0000', '2022-09-17 17:28:43', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (771773392977272836, 189, 771740975218434048, 771740975411372032, '1.0000', '2022-10-31 16:31:54', '0724');
INSERT INTO "public"."windows_functions" ("vaf01", "bbx01", "vaa01", "vaf06", "vaf21", "vaf36", "bce02a") VALUES (871354775944450049, 189, 871333633741451264, 871333635742134272, '1.0000', '2023-08-02 11:32:47', '0724');

窗口函数 row_number
SELECT vaf01,bbx01,vaa01,vaf06,vaf21,vaf36,bce02a,row_number() OVER (PARTITION BY bbx01 ORDER BY vaf36 DESC)
FROM windows_functions;
说明:在 bbx01 相同的情况下,按照 vaf36 进行倒序排序,选择时间最大的排在第一。可以看到 row_number 列是没有重复值的,即使 bbx01 相等并且 vaf36 也相等的情况下 row_number 的值也是不相等的。

窗口函数 rank
SELECT vaf01,bbx01,vaa01,vaf06,vaf21,vaf36,bce02a,rank() OVER (PARTITION BY bbx01 ORDER BY vaf36 DESC)
FROM windows_functions;
说明:在 bbx01 相同的情况下,按照 vaf36 进行倒序排序,选择时间最大的排在第一,可以看到 rank 列是有重复值的,当 bbx01 相等并且 vaf36 也相等的情况下 rank 的值是相当的。但是 rank 的值是存在跳跃的比如 1 2 2 4 会存在没有 3 的情况。

窗口函数 dense_rank
SELECT vaf01,bbx01,vaa01,vaf06,vaf21,vaf36,bce02a,dense_rank() OVER (PARTITION BY bbx01 ORDER BY vaf36 DESC)
FROM windows_functions;
说明:其与 rank 的值是类似的,但是最后的列 dense_rank 不存在跳跃。

窗口函数 first_value
SELECT vaf01,bbx01,vaa01,vaf06,vaf21,vaf36,bce02a,first_value(vaf36) OVER (PARTITION BY bbx01 ORDER BY vaf36 DESC)
FROM windows_functions;
说明:获取当 bbx01 相等的情况下,vaf36 倒序排序的第一个值也就是最大值。

窗口函数 last_value
说明:窗口函数 first_value 与窗口函数 last_value 是类似的。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2240996.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Postgresql源码(138)alter table增加列的执行流程分析

alter table 逻辑比较繁琐,但并不复杂,这里以增加列为例简单梳理流程。 测试用例 drop table t_echo; create table t_echo(a int,b int); insert into t_echo select t.i, t.i*10 from generate_series(1,10) t(i); alter table t_echo add c varchar…

MobaXterm 软件及如何设置取消自动断开连接

MobaXterm 软件及如何设置取消自动断开连接 背景软件资料取消自动断开连接找打设置找到SSH Setting 背景 其实很容易找到类似的资料, 主要为了简单的记录一下, 还是写一下这个博客吧, 也算是自己的输出了. 当练习打字也行吧, 反正自己打字也是一坨, 打字太慢了. 软件资料 官…

【代码审计】常见漏洞专项审计-业务逻辑漏洞审计

❤️博客主页: iknow181 🔥系列专栏: 网络安全、 Python、JavaSE、JavaWeb、CCNP 🎉欢迎大家点赞👍收藏⭐评论✍ 0x01 漏洞介绍 1、 原理 业务逻辑漏洞是一类特殊的安全漏洞,业务逻辑漏洞属于设计漏洞而非实…

YOLOv11实战垃圾分类

本文采用YOLOv11作为核心算法框架,结合PyQt5构建用户界面,使用Python3进行开发。YOLOv11以其高效的特征提取能力,在多个图像分类任务中展现出卓越性能。本研究针对4种垃圾数据集进行训练和优化,该数据集包含丰富的垃圾图像样本&am…

Kafka集群中数据的存储是按照什么方式存储的?

1)Topic 数据的存储机制 Topic是逻辑上的概念,而partition是物理上的概念,每个partition对应于一个log文件,该log文件中存储的就是Producer生产的数据。Producer生产的数据会被不断追加到该log文件末端,为防止log文件…

渗透测试靶场 - vulhub -CVE漏洞 Weblogic 任意文件上传漏洞(CVE-2018-2894) 漏洞讲解 漏洞复现 亲测步骤能通过哟

漏洞说明 Weblogic管理端未授权的两个页面存在任意上传jsp文件漏洞,可直接获取服务器权限。 两个页面 /ws_utc/begin.do /ws_utc/config.do Oracle 在2018年7月更新中,修复了Weblogic Web Service Test Page中一处任意文件上传漏洞, Web Se…

使用 PyTorch 实现 AlexNet 进行 MNIST 图像分类

AlexNet 是一种经典的深度学习模型,它在 2012 年的 ImageNet 图像分类比赛中大放异彩,彻底改变了计算机视觉领域的格局。AlexNet 的核心创新包括使用深度卷积神经网络(CNN)来处理图像,并采用了多个先进的技术如 ReLU 激…

基于图的去中心化社会推荐过滤器

🏡作者主页:点击! 🤖编程探索专栏:点击! ⏰️创作时间:2024年11月11日19点20分 点击开启你的论文编程之旅https://www.aspiringcode.com/content?id17176636216843&uideba758a1550b46bb…

深度学习模型评价指标介绍

模型评价指标 模型评价指标1.混淆矩阵2.Overall Accuracy3.Average accuracy4.Kappa系数5.Recall6.Precision7.F18.PR曲线9.置信度10.IOU11.AP12.mAP 模型评价指标 在我们学习机器学习以及深度学习,甚至在计算机视觉领域,我们不可避免的要利用一些指标评…

k8s 1.28.2 集群部署 docker registry 接入 MinIO 存储

文章目录 [toc]docker registry 部署生成 htpasswd 文件生成 secret 文件 生成 registry 配置文件创建 service创建 statefulset创建 ingress验证 docker registry docker registry 监控docker registry ui docker registry dockerfile docker registry 配置文件 S3 storage dr…

【自用】0-1背包问题与完全背包问题的Java实现

引言 背包问题是计算机科学领域的一个经典优化问题,分为多种类型,其中最常见的是0-1背包问题和完全背包问题。这两种问题的核心在于如何在有限的空间内最大化收益,但它们之间存在一些关键的区别:0-1背包问题允许每个物品只能选择…

Zookeeper的安装与使用

一、简介 1.1、概念 ZooKeeper 是一个开源的分布式协调服务,主要用于解决分布式系统中的数据一致性问题。它提供了一种可靠的机制来管理和协调分布式系统的各个节点。ZooKeeper 的设计目标是简化分布式应用的开发,提供简单易用的接口和高性能、高稳定性…

【模块一】kubernetes容器编排进阶实战之etcd的介绍与使用

etcd进阶 etcd简介:  etcd是CoreOS团队于2013年6月发起的开源项目,它的目标是构建一个高可用的分布式键值(key-value)数据库。etcd内部采用raft协议作为一致性算法,etcd基于Go语言实现。 官方网站:https://etcd.io/  gith…

【机器学习】如何配置anaconda环境(无脑版)

马上就要上机器学习的实验,这里想写一下我配置机器学习的anaconda环境的二三事 一、首先,下载安装包: Download Now | Anaconda 二、打开安装包,一直点NEXT进行安装 这里要记住你要下载安装的路径在哪,后续配置环境…

矩阵中的路径(dfs)-acwing

题目 23. 矩阵中的路径 - AcWing题库 代码 class Solution { public://以每一个坐标作为dfs起点bool hasPath(vector<vector<char>>& matrix, string str) {for (int i 0; i < matrix.size(); i )for (int j 0; j < matrix[i].size(); j )if (dfs(…

WEB攻防-通用漏洞SQL注入sqlmapOracleMongodbDB2等

SQL注入课程体系&#xff1a; 1、数据库注入-access mysql mssql oracle mongodb postgresql 2、数据类型注入-数字型 字符型 搜索型 加密型&#xff08;base64 json等&#xff09; 3、提交方式注入-get post cookie http头等 4、查询方式注入-查询 增加 删除 更新 堆叠等 …

android studio 更改gradle版本方法(备忘)

如果出现类似以下&#xff1a; Your build is currently configured to use Java 17.0.11 and Gradle 6.1.1. 或者类似&#xff1a; Failed to calculate the value of task ‘:app:compileDebugJavaWithJavac‘ property ‘options.generatedSo 消息时需要修改gradle版本&…

设计模式之装饰器模式(SSO单点登录功能扩展,增加拦截用户访问方法范围场景)

前言&#xff1a; 两个本想描述一样的意思的词&#xff0c;只因一字只差就让人觉得一个是好牛&#xff0c;一个好搞笑。往往我们去开发编程写代码时也经常将一些不恰当的用法用于业务需求实现中&#xff0c;但却不能意识到。一方面是由于编码不多缺少较大型项目的实践&#xff…

日志:中文 URI 参数乱码之 encodeURI、encodeURIComponent、escape 作为 Ajax 中文参数编码给 ASP 的记录

前面提到的了 ASP 输出 UTF-8 编码的中文不定时出现乱码的解决方案&#xff1a;ASP页面改为UTF-8编码后&#xff0c;刷新页面中文输入输出不定时乱码终极解决方案 今天遇到的则是输入 UTF-8 编码中文 URI 参数乱码的问题&#xff0c;第一次可以&#xff0c;刷新后取得的输入参…

Intern大模型训练营(八):Llamaindex RAG 实践

1. 基于 LlamaIndex 构建自己的 RAG 知识库 首先在Intern Studio中申请30% A100的开发机。 进入开发机后&#xff0c;创建新的conda环境&#xff0c;命名为 llamaindex&#xff0c;在命令行模式下运行&#xff1a; conda create -n llamaindex python3.10 复制完成后&#…