PostgreSQL equivalent for TOP n WITH TIES: LIMIT “with ties”?

后端 未结 3 1829
遥遥无期
遥遥无期 2020-11-29 09:00

I\'m looking for something similar this in SQL Server:

SELECT TOP n WITH TIES FROM tablename

I know about LIMIT in PostgreSQL,

3条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 09:29

    PostgreSQL already supports OFFEST FETCH clause and starting from version 13 it will support FETCH FIRST WITH TIES:

    SELECT

    [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } { ONLY | WITH TIES } ]
    

    The WITH TIES option is used to return any additional rows that tie for the last place in the result set according to the ORDER BY clause; ORDER BY is mandatory in this case.

    Query:

    SELECT nums 
    FROM Numbers 
    ORDER BY nums DESC
    FETCH NEXT 3 ROWS WITH TIES;
    

    db<>fiddle demo

提交回复
热议问题