sql - PostgreSQL equivalent for TOP n WITH TIES: LIMIT "with ties"? -
i'm looking similar in sql server:
select top n ties tablename
i know limit
in postgresql, equivalent of above exist? i'm curious save query each time me.
if have table numbers
attribute nums
: {10, 9, 8, 8, 2}
. want like:
select nums numbers order nums desc limit *with ties* 3
it should return {10, 9, 8, 8}
because takes top 3 plus 8
since ties other one.
there no with ties
clause in postgresql there in sql server.
in postgresql substitute top n ties .. order <something>
:
with cte ( select *, rank() on (order <something>) rnk tbl ) select * cte rnk <= n;
to clear, rank()
right, dense_rank()
wrong (return many rows).
consider quote sql server docs (from link above):
for example, if expression set 5 2 additional rows match values of order columns in row 5, result set contain 7 rows.
the job of with ties
include peers of last row in top n defined order by
clause. rank()
gives exact same result.
to make sure, tested sql server, here live demo.
, here more convenient sqlfiddle.
Comments
Post a Comment