Re: Generate a dynamic sequence within a query

From: Josh Kupershmidt <schmiddy(at)gmail(dot)com>
To: David Kerr <dmk(at)mr-paradox(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Generate a dynamic sequence within a query
Date: 2010-10-21 02:32:15
Message-ID: AANLkTim+xbPfKhcK3FdOfxfzR63Fu8_P4rUzcBncZaPK@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Oct 20, 2010 at 6:22 PM, David Kerr <dmk(at)mr-paradox(dot)net> wrote:
> I know I've seen posts on how to do this, but i can't seem to find them.
>
> I've got a data set
>
> A, B
> A, C
> A, D
> [...]
>
> and so on
>
> and i'd like to be able to wite a query that would result in
>
> 1,A,B
> 2,A,C
> 3,A,D
> [...]
>
> PG version is 8.3.

If you can upgrade to 8.4, you could use the row_number() window
function which is perfectly suited to this task, should be as simple
as:

SELECT row_number() OVER (), * FROM tablename;

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2010-10-21 02:32:29 Re: Custom cache implemented in a postgresql C function
Previous Message Tom Lane 2010-10-21 02:28:34 Re: Composite Index question