PostgreSQL的DISTINCT关键字用于与SELECT语句消除所有重复的记录,并获取唯一记录。有可能的情况下,当你有多个重复的表中的记录。虽然取这样的记录,它更有意义,获取唯一的记录,而不是获取重复记录。

语法:

    DISTINCT关键字消除重复记录的基本语法如下:

    SELECT DISTINCT column1, column2,.....columnN FROM table_name WHERE [condition]

案例:

    1、去重;关键字distinct去重功能  在其他数据库(oracle、mysql、db2、informix)也有。

        select distinct idfa from nlogs where recvtime>='2015-09-01 00:00:00' and recvtime<'2015-09-03 00:00:00';

    2、统计不重复个数

        select count( distinct( idfa ) ) from nlogs where recvtime>='2015-09-01 00:00:00' and recvtime<'2015-09-03 00:00:00';

额外:

    distinct跟on一起用; 使用DISTINCT ON实现用窗口函数实现的取第一名的功能,这个功能oracle,mysql是没有的;当然它们有其他的分析函数可以替换;顶替;例如row_number, fisrt_values等。

    获取每个idfa最新接收时间:

    select distinct on (idfa) idfa, recvtime from nlogs where recvtime>='2015-09-01 00:00:00' and recvtime<'2015-09-03 00:00:00';

    参阅:https://www.cnblogs.com/lottu/p/5553588.html

01-11 19:07