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)