SQL to find the first occurrence of sets of data in a table

前端 未结 4 1437
执念已碎
执念已碎 2021-02-09 14:48

Say if I have a table:

CREATE TABLE T
(
    TableDTM  TIMESTAMP  NOT NULL,
    Code      INT        NOT NULL
);

And I insert some rows:

4条回答
  •  [愿得一人]
    2021-02-09 15:04

    PostgreSQL supports window functions, have a look at this

    [EDIT] Try the following:

    SELECT TableDTM, Code FROM
    (
        SELECT TableDTM,
               Code,
               LAG(Code, 1, NULL) OVER (ORDER BY TableDTM) AS PrevCode
        FROM   T
    )
    WHERE PrevCode<>Code OR PrevCode IS NULL;
    

提交回复
热议问题