Re: Probably simple answer - Mailing list pgsql-general

From Al Kirkus
Subject Re: Probably simple answer
Date
Msg-id sbea269b.029@dist102.k12.il.us
Whole thread Raw
In response to Probably simple answer  ("Al Kirkus" <Al@dist102.k12.il.us>)
List pgsql-general
Thanks.
That just might work!
Al

>>> Andrew Sullivan <andrew@libertyrms.info> - 11/6/01 12:12 PM >>>
On Tue, Nov 06, 2001 at 07:41:06AM -0800, Andrew Gould wrote:
> I think he simply wants the rows of the result set
> numbered from 1 to whatever the last row is.  This is
> useful for referencing rows when discussing reports.
> It is also an immense help when calculating a record's
> statistical percentile ranking.
>
> Doing it in a table is no problem.  But how do you
> create one dynamically in a select query?

What about using a sequence inside a transaction:

scratch=# \d tmp1
        Table "tmp1"
 Attribute | Type | Modifier
-----------+------+----------
 col1      | text |

scratch=# begin ;
BEGIN
scratch=# create SEQUENCE temp_seq minvalue 1 increment 1;
CREATE
scratch=# select nextval('temp_seq') as rownum, col1 from tmp1 ;
 rownum | col1
--------+------
      1 | a
      2 | b
      3 | c
      4 | d
      5 | e
      6 | f
(6 rows)

scratch=# rollback;
ROLLBACK

The ROLLBACK gets rid of the sequence, so you don't have it hanging
around, and since you're in a transaction, no-one else can see your
sequence, so it won't get incremented by someone else calling to it.
Not perfect, but for on-the-fly row numbering, it might work.

--
----
Andrew Sullivan                               87 Mowat Avenue
Liberty RMS                           Toronto, Ontario Canada
<andrew@libertyrms.info>                              M6K 3E3
                                         +1 416 646 3304 x110


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)


pgsql-general by date:

Previous
From: Fernando Schapachnik
Date:
Subject: Re: Caching query plan...
Next
From: Martín Marqués
Date:
Subject: Re: [HACKERS] PostgreSQL v7.2b2 Released