意思是DISTINCT ON ( expression [, …] )把记录根据[, …]的值进行分组,分组之后仅返回每一组的第一行。
需要注意的是,如果你不指定ORDER BY子句,返回的第一条的不确定的。如果你使用了ORDER BY 子句,
那么[, …]里面的值必须靠近ORDER BY子句的最左边。
postgres=# CREATE TABLE score_ranking (id int, name text, subject text, score numeric);

postgres=# INSERT INTO score_ranking VALUES (1,'killerbee','数学',99.5), (2,'killerbee','语文',89.5),(3,'killerbee','英语',79.5), (4,'killerbee','物理',99.5), (5,'killerbee','化学',98.5),(6,'刘德华','数学',89.5), (7,'刘德华','语文',99.5), (8,'刘德华','英语',79.5),(9,'刘德华','物理',89.5), (10,'刘德华','化学',69.5),(11,'张学友','数学',89.5), (12,'张学友','语文',91.5), (13,'张学友','英语',92.5),(14,'张学友','物理',93.5), (15,'张学友','化学',94.5);
//先查看下当前表的内容。
postgres=# select * from score_ranking ;
id | name | subject | score
----+-----------+---------+-------
1 | killerbee | 数学 | 99.5
2 | killerbee | 语文 | 89.5
3 | killerbee | 英语 | 79.5
4 | killerbee | 物理 | 99.5
5 | killerbee | 化学 | 98.5
6 | 刘德华 | 数学 | 89.5
7 | 刘德华 | 语文 | 99.5
8 | 刘德华 | 英语 | 79.5
9 | 刘德华 | 物理 | 89.5
10 | 刘德华 | 化学 | 69.5
11 | 张学友 | 数学 | 89.5
12 | 张学友 | 语文 | 91.5
13 | 张学友 | 英语 | 92.5
14 | 张学友 | 物理 | 93.5
15 | 张学友 | 化学 | 94.5
(15 rows) //以下就是 distict on 的用法:
//取出每门课程的第一名.
postgres=# select distinct on (subject) id,name,subject,score
from score_ranking
order by subject,score desc; 备注:逻辑是: 先依据subject进行分组,比如如果subject有五类,那就分成5组。其次,score desc决定每一组内部排序,最终决定每组取谁。 id | name | subject | score
----+-----------+---------+-------
5 | killerbee | 化学 | 98.5
1 | killerbee | 数学 | 99.5
4 | killerbee | 物理 | 99.5
13 | 张学友 | 英语 | 92.5
7 | 刘德华 | 语文 | 99.5
(5 rows)

order by的时候,按照上面的说法

"如果你使用了ORDER BY 子句, 那么[, …]里面的值必须靠近ORDER BY子句的最左边。"
select distinct on (subject),所以根据分数排序的时候,本来应该是order by score,而[, …]里面的值(subject)必须靠近ORDER BY子句的最左边,
所以是 order by subject,score desc;
												

postgre dinstinct on()的使用的更多相关文章

  1. Postgre cannot insert multiple commands into a prepared statement

    悲剧... FireDAC连接Postgre数据库, 使用默认的属性, 一次执行多条SQL的时候, 会报"cannot insert multiple commands into a pre ...

  2. postgre去重复记录

    postgre去重复记录,主要用到row定位的一个系统表示 “ctid”,能查出纯净的不重复的记录,那要删掉重复值也就容易了,自己去折腾吧. 我所涉及的是得到不重复的记录,就一句话: select c ...

  3. what a fuck postgre update sql

    ================= what a fuck postgre update sql ================= UPDATE temp_group_temp set group_ ...

  4. Windows下Postgre SQL数据库通过Slony-I 实现数据库双机同步备份

    一. 我们要实现的环境是windows xp.windows2003上安装Postgre SQL数据库,实现目的是两台数据库服务器进行数据库同步,即数据库同步更新.删除.插入等对数据库的操作. 二. ...

  5. HOW TO: How to import UUID function into Postgre 9.3

    1. Open a command console and go to the directory where you installed Postgre server. e.g. D:\Progra ...

  6. 使用postgre数据库实现树形结构表的子-父级迭代查询,通过级联菜单简单举例

    前言:开发常用的关系型数据库MySQL,mssql,postgre,Oracle,简单的增删改查的SQL语句都与标准SQL兼容,这个不用讲,那么对于迭代查询(不严格的叫法:递归查询)每种数据库都不一样 ...

  7. Postgre: How to import UUID function into Postgre 9.3

    1. Open a command console and go to the directory where you installed Postgre server. e.g. D:\Progra ...

  8. Postgre Sql获取最近一周、一月、一年日期函数

    使用Postgre Sql获取近一周.一年.一月等系统函数调用如下,使用方面相对于Ms Sql server 容易了许多. --当前时间 select now(); --current_timesta ...

  9. postgre 常用语法,如 group_concat用法

    1.查询postgre的表所有字段列 select table_name, column_name from information_schema.columns where table_schema ...

随机推荐

  1. vue.js实战——计算属性

    1set和get: 注意: this.lastName=names[names.length-1];//解决连续输入空格后lastName消失的问题 练习代码如下: <!DOCTYPE html ...

  2. PLSQL:orecal,tnsname简介

    导入ORACLE遇到很多问题,学了好多,其中很长时间花在网络配置上,刚开始学,具体原因不知道,先把搜集到的好文章存下来,以后慢慢研究. 监听配置文件             为了使得外部进程 如 CA ...

  3. Python——网络编程基础

    一.TCP/IP 是Internet的基础协议,分四层应用层(HTTP,SMTP),传输层(TCP/UDP),网络层(IP),接口层 二.常用默认端口号 80-TCP-HTTP 23-TCP-TELN ...

  4. css---遮罩层

    <div id="body"> 显示页面的全部内容 <div id="open">打开弹框</div> </div&g ...

  5. python基础之小数据池、代码块、编码和字节之间换算

    一.代码块.if True: print(333) print(666) while 1: a = 1 b = 2 print(a+b) for i in '12324354': print(i) 虽 ...

  6. NetSarang软件中nssock2.dll模块被植入恶意代码技术分析与防护方案

    原文地址:http://blog.nsfocus.net/nssock2-dll-module-malicious-code-analysis-report/ NetSarang是一家提供安全连接解决 ...

  7. ubuntu不能联网的问题

    控制面板\网络和 Internet\网络和共享中心-->更改>配器设置-->以太网-->右键属性-->共享-->允许其他网络用户通过此计算机的internet连接来 ...

  8. Hadoop记录- Yarn scheduler队列采集

    #!/bin/sh ip=10.116.100.11 port=8088 export HADOOP_HOME=/app/hadoop/bin rmstate1=$($HADOOP_HOME/yarn ...

  9. Quartz.net 3.x使用总结(一)——入门介绍

    1.Quartz.net简介 Quartz.NET是一个强大.开源.轻量级的任务调度框架.任务调度在我们的开发中经常遇到,如说:每天晚上三点让程序或网站执行某些代码,或者每隔5秒种执行一个方法等.Wi ...

  10. LFYZ-OJ ID: 1015 统计数字(NOIP2007)

    分析 本体思路很简单:读入数据,排序.统计.输出.难点在于数据量较大,选择何种排序方法就极为重要,否则很容易发生内存或时间超限.可以考虑以下几种思路: 桶排序 桶排序是可以想到的最简单方法,可在O(n ...