Re: Re: Not able to find an alternative for SELECT TOP n WITH TIES in postgresql which is available in sql server.

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: "MAJUMDER, SAYAN" <sayan(dot)a(dot)majumder(at)capgemini(dot)com>, "pgsql-admin(at)postgresql(dot)org" <pgsql-admin(at)postgresql(dot)org>
Subject: Re: Re: Not able to find an alternative for SELECT TOP n WITH TIES in postgresql which is available in sql server.
Date: 2017-05-20 00:32:47
Message-ID: CAKFQuwYCrHfszvko60Z3rN6=_eye4hk3Ng-tcA=72UJ7yANaQA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Fri, May 19, 2017 at 5:14 PM, Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
wrote:

> I think the answer to your question is to
> use a window function.
>
> MAJUMDER, SAYAN wrote on April 24th 2017:
>
> > Hi,
> > I am new to postgresql and we have a query in sql server that is SELECT
> TOP n WITH TIES column_name from table_name.
> > This clause is used to retrieve all similar rows to a base result set.
> > I am not able to find any similar clause in postgresql.
> >
> > Example in sql server:-
> > We have a table with 6 entries 1 to 4 and 5 twice.
> >
> > SELECT TOP 5 WITH TIES *
> > FROM MyTable
> > ORDER BY ID;
> >
> > This will returns 6 rows, as the last row is tied (exists more than
> once.)
> >

​Alvaro is right - though you also need a subquery.​

​​select * from (
select dense_rank() OVER (ORDER BY v)
from (values (1),(2),(3),(4),(5),(5)) vals (v)
) src where dense_rank < 6

​David J.​

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Sumeet Shukla 2017-05-22 05:56:49 Re: error installing oracle_fdw extension
Previous Message Alvaro Herrera 2017-05-20 00:14:50 Re: Not able to find an alternative for SELECT TOP n WITH TIES in postgresql which is available in sql server.