1. 去重;关键字distinct去重功能  在其他数据库(oracle,mysql)是存在;当然postgresql也有这个功能

[postgres@sdserver40_210 ~]$ psql mydb lottu
psql (9.5.)
Type "help" for help. mydb=> select * from trade;
tradeno | accountid | fee | game_id
---------+------------+-----+---------
| yyb_100001 | |
| yyb_100002 | |
| yyb_100001 | |
| yyb_100003 | |
| yyb_100004 | |
| yyb_100002 | |
( rows) mydb=> select distinct accountid from trade;
accountid
------------
yyb_100001
yyb_100004
yyb_100002
yyb_100003
( rows) mydb=> select distinct accountid,game_id from trade;
accountid | game_id
------------+---------
yyb_100001 |
yyb_100003 |
yyb_100004 |
yyb_100002 |
( rows)
2. 跟on一起用; 使用DISTINCT ON实现用窗口函数实现的取第一名的功能
    这个功能oracle,mysql是没有的;当然它们有其他的分析函数可以替换;顶替;例如row_number, fisrt_values等等
mydb=> select distinct on (accountid) accountid,fee from trade;
accountid | fee
------------+-----
yyb_100001 |
yyb_100002 |
yyb_100003 |
yyb_100004 |
( rows) mydb=> select distinct on (game_id) accountid,fee from trade;
accountid | fee
------------+-----
yyb_100001 |
( row) mydb=> select distinct on (game_id) accountid,fee from trade order by game_id, fee desc;
accountid | fee
------------+-----
yyb_100002 |
( row) --例如取每个帐号充值最大的一笔
mydb=> select distinct on (accountid) accountid,fee from trade order by accountid, fee desc;
accountid | fee
------------+-----
yyb_100001 |
yyb_100002 |
yyb_100003 |
yyb_100004 |
( rows)

postgresql之distinct用法的更多相关文章

  1. 转一个distinct用法,很有帮助

    转一个distinct用法,很有帮助 (2011-12-01 15:18:11) 转载▼ 标签: 杂谈 分类: mysql复制 在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提 ...

  2. mysql distinct 用法详解及优化

    本事例实验用表task,结构如下 MySQL> desc task; +-------------+------------+------+-----+-------------------+- ...

  3. PostgreSQL>窗口函数的用法

    PostgreSQL之窗口函数的用法 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9311281.html PostgreSQL的高级特性本准备三篇的(递归. ...

  4. distinct用法

    distinct可以列出不重复的记录,对于单个字段来说distinct使用比较简单,但是对于多个字段来说,distinct使用起来会使人发狂.而且貌似也没有见到微软对distinct使用多字段的任何说 ...

  5. [Irving]SQL去重复-DISTINCT用法

    在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值.关键词 distinct用于返回唯一不同的值. 表A: 示例1 select distinct nam ...

  6. PostgreSQL 的 distinct on 的理解

    摘录自:http://www.cnblogs.com/gaojian/archive/2012/09/05/2671381.html 对于 select distinct on , 可以利用下面的例子 ...

  7. mysql distinct()用法

    在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所 ...

  8. Pig distinct用法举例

    dst = distinct data:   DISTINCT只能对整个记录(整行)去重,不能在字段级别去重.   触发reduce阶段   data = load 'data'; distinct ...

  9. mysql中去重 distinct 用法

    在使用MySQL时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count( ...

随机推荐

  1. [NetTopologySuite](2)任意多边形求交

    任意多边形求交: private void btnPolygon_Click(object sender, EventArgs e) { , , , , , , , , , , , , , }; , ...

  2. Visual Studio Online

    删除Visual Studio Online的项目http://taslimi.me/how-to-delete-a-team-project-from-tfs-online-tfs.visualst ...

  3. maven中下载jar包源码和javadoc

    1:Maven命令下载源码和javadocs 当在IDE中使用Maven时如果想要看引用的jar包中类的源码和javadoc需要通过maven命令下载这些源码,然后再进行引入,通过mvn命令能够容易的 ...

  4. Lintcode: Matrix Zigzag Traversal

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in ZigZag-or ...

  5. ngrok外网登录本地Web服务器

    首先在网上下载ngrok软件,然后cmd到其目录下,运行ngrok http 80即可打开服务器,然后自动生成外网连接,然后C:\inetpub\wwwroot下放置html网页,在公网即可打开

  6. 【IOS】3. OC 类声明和实现

    .h文件 @interface NewClassName:ParentClassName { 实例变量://基本类型和指针类型  不能在这里初始化,系统默认会初始化 系统初始化遵循: 实例变量类型   ...

  7. ubuntu安装jdk遇到的问题:cannot execute binary file

    安装完jdk,配置好环境变量出现如下状况: cannot execute binary file 问题原因: jdk的位数与ubuntu的系统位数不一致 jdk 64位 ubuntu 32位 然后通过 ...

  8. android 添加背景音乐

    MediaPlayer mediaPlayer=MediaPlayer.create(MainActivity.this,R.raw.qiji); mediaPlayer.start();

  9. Spring之我见

    Spring 是什么(1) •Spring 是一个开源框架. •Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能. • ...

  10. mysql linux 备份脚本

    #!/bin/sh # mysql data backup script # # use mysqldump --help,get more detail. # BakDir=/root/back/m ...