Postgresql 窗口函数示例
来源:互联网 发布:windows域 编辑:程序博客网 时间:2024/06/06 05:47
数据准备
drop table scores;
create table scores
(
id int ,
lesson varchar(12),
score int);
insert into scores(id,lesson,score) values(1,'match',90);
insert into scores(id,lesson,score) values(2,'match',85);
insert into scores(id,lesson,score) values(3,'match',85);
insert into scores(id,lesson,score) values(4,'match',95);
insert into scores(id,lesson,score) values(5,'english',66);
insert into scores(id,lesson,score) values(6,'english',88);
insert into scores(id,lesson,score) values(7,'physics',78);
insert into scores(id,lesson,score) values(8,'physics',70);
insert into scores(id,lesson,score) values(9,'physics',80);
insert into scores(id,lesson,score) values(10,'physics',95);
insert into scores(id,lesson,score) values(11,'physics',95);
操作
1.row_number():返回行号,对比值重复时行号不重复不间断, 即返回 1,2,3,4,5....,不返回 1,2,2,4...
select row_number() over(),* from scores limit 2;
select row_number() over(),* from scores limit 2offset 2;
--按lesson分组,score排序,注意红色记录行号不间断
select row_number()over(partitionby lessonorder by score),* from scores;
2.rank():返回行号,对比值重复时行号重复并间断, 即返回 1,2,2,4...
select rank()over(partitionby lessonorder by score),* from scores;
3.dense_rank():返回行号,对比值重复时行号重复但不间断, 即返回 1,2,2,3
select dense_rank()over(partitionby lessonorder by score),* from scores;
4.percent_rank():从当前开始, 计算在分组中的比例 (行号-1)*(1/(总记录数-1))
select percent_rank()over(partition by lessonorderby score),*from scores;
- Postgresql 窗口函数示例
- Postgresql窗口函数
- postgresql 窗口函数
- PostgreSQL 窗口函数复习笔记
- postgresql关联更新与窗口函数应用
- 子窗口调用父窗口变量和函数示例
- PostgreSql聚合函数二---聚合函数,分析函数和窗口函数
- postgresql 函数
- postgreSQL函数
- PostgreSQL-函数
- postgresql函数
- Postgresql 函数
- C连接PostgreSQL示例
- ROWNUMBER、RANK、DENSE_RANK、NTILE排名窗口函数示例
- PostgreSQL游标示例(创建游标,并在函数中遍历之)
- PostgreSQL C风格的自定义函数FuncCallContext结构体描述及示例
- MSSQL示例(三)ROW_NUMBER、RANK、DENSE_RANK、NTILE排名窗口函数示例
- PostgreSQL 备份与恢复示例
- VC++6.0插件系列(绝对经典)
- PAT a1006题解
- 写给前端工程师的理论基础(2)--HTTPS协议与密码学
- 2、成员内部类常用修饰符(private和static)
- linux 常用命令
- Postgresql 窗口函数示例
- oracle数据库字符格式修改
- python学习之模块
- Web前端知识体系精简
- PAT a1008题解
- 数据挖掘概念与技术-阅读笔记1
- 多线程之循环栅栏CyclicBarrier及原理
- PAT a 1009题解
- PMP 第4章 项目整合管理