Re: how to get row number in select query

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: how to get row number in select query
Date: 2011-01-27 15:30:02
Message-ID: ihs31q$1lm$1@dough.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Piotr Czekalski, 27.01.2011 16:21:
> Gentelmen,
>
> I follow this thread and I don't exactly get an idea of yours, but
> isn't is as simple as (example: table "web.files" contains one column
> named "fileurl" ):
>
> select row_number() over(), X.fileurl from (select fileurl from
> web.files order by fileurl) X
>
> The only disadvantage is that if you do want to order resultset you
> have to use "select from select" as numbers are added before order
> which may cause some performance troubles.
>

You can get the row_number() without using the sub-select and without ordering the whole result as you can specify the order in the over() clause:

select fileurl
row_number() over (order by fileurl)
from web.files

Regards
Thomas

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Amitabh Kant 2011-01-27 17:50:31 Automating PostgreSql table partition using triggers
Previous Message Piotr Czekalski 2011-01-27 15:21:06 Re: how to get row number in select query