PostgreSQL ROW_NUMBER() OVER()
转自:http://blog.csdn.net/luojinbai/article/details/45078809
语法:
ROW_NUMBER() OVER( [ PRITITION BY col1] ORDER BY col2[ DESC ] )
解释:
ROW_NUMBER()为返回的记录定义个行编号, PRITITION BY col1 是根据col1分组,ORDER BY col2[ DESC ]是根据col2进行排序。
举例:
postgres=# create table student(id serial,name character varying,course character varying,score integer);
CREATE TABLE
postgres=#
postgres=# \d student
Table "public.student"
Column | Type | Modifiers
--------+-------------------+----------------------------------------------
id | integer | not null default nextval('student_id_seq'::regclass)
name | character varying |
course | character varying |
score | integer |
insert into student (name,course,score) values('周润发','语文',89);
insert into student (name,course,score) values('周润发','数学',99);
insert into student (name,course,score) values('周润发','外语',67);
insert into student (name,course,score) values('周润发','物理',77);
insert into student (name,course,score) values('周润发','化学',87);
insert into student (name,course,score) values('周星驰','语文',91);
insert into student (name,course,score) values('周星驰','数学',81);
insert into student (name,course,score) values('周星驰','外语',88);
insert into student (name,course,score) values('周星驰','物理',68);
insert into student (name,course,score) values('周星驰','化学',83);
insert into student (name,course,score) values('黎明','语文',85);
insert into student (name,course,score) values('黎明','数学',65);
insert into student (name,course,score) values('黎明','外语',95);
insert into student (name,course,score) values('黎明','物理',90);
insert into student (name,course,score) values('黎明','化学',78);
1. 根据分数排序
postgres=# select *,row_number() over(order by score desc)rn from student;
id | name | course | score | rn
----+--------+--------+-------+----
2 | 周润发 | 数学 | 99 | 1
13 | 黎明 | 外语 | 95 | 2
6 | 周星驰 | 语文 | 91 | 3
14 | 黎明 | 物理 | 90 | 4
1 | 周润发 | 语文 | 89 | 5
8 | 周星驰 | 外语 | 88 | 6
5 | 周润发 | 化学 | 87 | 7
11 | 黎明 | 语文 | 85 | 8
10 | 周星驰 | 化学 | 83 | 9
7 | 周星驰 | 数学 | 81 | 10
15 | 黎明 | 化学 | 78 | 11
4 | 周润发 | 物理 | 77 | 12
9 | 周星驰 | 物理 | 68 | 13
3 | 周润发 | 外语 | 67 | 14
12 | 黎明 | 数学 | 65 | 15
(15 rows)
rn是给我们的一个排序。
2. 根据科目分组,按分数排序
postgres=# select *,row_number() over(partition by course order by score desc)rn from student;
id | name | course | score | rn
----+--------+--------+-------+----
5 | 周润发 | 化学 | 87 | 1
10 | 周星驰 | 化学 | 83 | 2
15 | 黎明 | 化学 | 78 | 3
13 | 黎明 | 外语 | 95 | 1
8 | 周星驰 | 外语 | 88 | 2
3 | 周润发 | 外语 | 67 | 3
2 | 周润发 | 数学 | 99 | 1
7 | 周星驰 | 数学 | 81 | 2
12 | 黎明 | 数学 | 65 | 3
14 | 黎明 | 物理 | 90 | 1
4 | 周润发 | 物理 | 77 | 2
9 | 周星驰 | 物理 | 68 | 3
6 | 周星驰 | 语文 | 91 | 1
1 | 周润发 | 语文 | 89 | 2
11 | 黎明 | 语文 | 85 | 3
(15 rows)
3. 获取每个科目的最高分
postgres=# select * from(select *,row_number() over(partition by course order by score desc)rn from student)t where rn=1;
id | name | course | score | rn
----+--------+--------+-------+----
5 | 周润发 | 化学 | 87 | 1
13 | 黎明 | 外语 | 95 | 1
2 | 周润发 | 数学 | 99 | 1
14 | 黎明 | 物理 | 90 | 1
6 | 周星驰 | 语文 | 91 | 1
(5 rows)
4. 每个科目的最低分也是一样的
postgres=# select * from(select *,row_number() over(partition by course order by score)rn from student)t where rn=1;
id | name | course | score | rn
----+--------+--------+-------+----
15 | 黎明 | 化学 | 78 | 1
3 | 周润发 | 外语 | 67 | 1
12 | 黎明 | 数学 | 65 | 1
9 | 周星驰 | 物理 | 68 | 1
11 | 黎明 | 语文 | 85 | 1
(5 rows)
只要在根据科目排序的时候按低到高顺序排列就好了。
PostgreSQL ROW_NUMBER() OVER()的更多相关文章
- postgresql中rank() over, dense_rank(), row_number() 的用法和区别
- SQL关于分页的sql查询语句 limit 和row_number() OVER函数
在做项目的时候需要些分页,用的数据库是mysql,之前看到的参考例子是用MS SQL做的,在MS SQL.ORACLE里面有ROW_NUMBER() OVER函数可以在数据库里对数据进行分组.百度后的 ...
- postgresql之distinct用法
1. 去重:关键字distinct去重功能 在其他数据库(oracle,mysql)是存在:当然postgresql也有这个功能 [postgres@sdserver40_210 ~]$ psql ...
- CYQ.Data 支持 PostgreSQL 数据库
前言: 很久之前,就有同学问我CYQ.Data能不能支持下PostgreSQL,之后小做了下调查,发现这个数据库用的人少,加上各种因素,就一直没动手. 前两天,不小心看了一下Github上的消息: 看 ...
- PostgreSQL>窗口函数的用法
PostgreSQL之窗口函数的用法 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9311281.html PostgreSQL的高级特性本准备三篇的(递归. ...
- postgresql语句
查询oracle数据库所有表数据量 select t.table_name,t.num_rows from user_tables t ORDER BY t.num_rows desc 查询postg ...
- SQL Server - 四种排序, ROW_NUMBER() /RANK() /DENSE_RANK() /ntile() over()
>>>>英文版 (更简洁易懂)<<<< 转载自:https://dzone.com/articles/difference-between-rownum ...
- postgresql 常用速查
中文资料 中文资料 /**gp中的基本sql语法**/ --删除表 drop table testtb; --创建表 CREATE TABLE testtb ( id integer, "n ...
- mysql、MS SQL关于分页的sql查询语句 limit 和row_number() OVER函数
在做项目的时候需要些分页,用的数据库是MySQL,之前看到的参考例子是用MS SQL做的,在MS SQL.Oracle里面有ROW_NUMBER() OVER函数可以在数据库里对数据进行分组.百度后的 ...
随机推荐
- redis-3.2.5 make 报错
make[]: Entering directory `/usr/local/src/redis-/src' CC adlist.o In file included : zmalloc.h::: e ...
- CSS3颜色渐变模式
1.线性渐变:linear-gradient 语法:<linear-gradient> = linear-gradient([ [ <angle> | to <si ...
- setTimeout使用闭包功能,实现定时打印数值
我们这次使用setTimeout来实现一个按照时间定时,依次打印数值的例子.其实在早期的时候,也是我经常犯的一个错误,或者实现这种能力,似乎js比较牵强,其实是我的错,哈哈!没能理解JS强大之处.我们 ...
- shell不能执行su 后的脚本
问题:在shell脚本中执行“su – 用户名”后,脚本终止执行,并且切换到su 中指定用户名的交互式界面 现象:我在root中执行一个脚本,但是其中的一些命令或脚本必须用oracle用户来执行., ...
- 9.JAVA中的正则表达式
一.JAVA中的正则表达式 1.概念:以某种特定的方式描述字符串 1.Java中正则表达式的规则 ? #{0,1}-?有一个-或者没有 \\ #表示一个" ...
- DELL灵越15R5521安装黑苹果
按照网上的流程安装即可:(懒人法) 首先分出两个硬盘分区,一个10G左右(用于做系统),一个30G左右(用于装系统)://注意生成时选择不要格式化 然后利用硬盘助手将镜像文件(.cdr文件)写入10G ...
- ASP.NET常见面试题及答案(130题)
1.C#中 property 与 attribute(抽像类)的区别,他们各有什么用处,这种机制的好处在哪里?答:property和attribute汉语都称之为属性.不过property是指类向外提 ...
- 实现倒计时功能js
<p>系统将会在<strong id="endtime"></strong>秒后跳转到登录页!</p> [原生js实现] <s ...
- Redis的PHP操作手册(转)
String 类型操作 string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象 $redis-> ...
- 深入浅出JMS(四)--Spring和ActiveMQ整合的完整实例
第一篇博文深入浅出JMS(一)–JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文深入 ...