Received: from anchor-post-31.mail.demon.net (anchor-post-31.mail.demon.net [194.217.242.89]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f3BGkP811841 for ; Wed, 11 Apr 2001 12:46:26 -0400 (EDT) (envelope-from olly@lfix.co.uk) Received: from lfix.demon.co.uk ([158.152.59.127] helo=linda.lfix.co.uk) by anchor-post-31.mail.demon.net with esmtp (Exim 2.12 #1) id 14nNl9-000MdA-0V; Wed, 11 Apr 2001 17:46:23 +0100 Received: from lfix.co.uk (olly@localhost [127.0.0.1]) by localhost (8.11.3/8.11.3/Debian 8.11.2-1) with ESMTP id f3BGkM205840; Wed, 11 Apr 2001 17:46:22 +0100 Message-Id: <200104111646.f3BGkM205840@linda.lfix.co.uk> X-Mailer: exmh version 2.3.1 01/18/2001 (debian 2.3.1-1) with nmh-1.0.4+dev X-URL: http://www.lfix.co.uk/oliver X-face: "xUFVDj+ZJtL_IbURmI}!~xAyPC"Mrk=MkAm&tPQnNq(FWxv49R}\>0oI8VM?O2VY+N7@F- KMLl*!h}B)u@TW|B}6 cc: "Koen Antonissen" , pgsql-sql@postgresql.org Subject: Re: enumerating rows In-reply-to: Message from Kovacs Zoltan of Wed, 11 Apr 2001 16:37:20 +0200. Mime-Version: 1.0 Content-Type: multipart/mixed ; boundary="==_Exmh_3126653160" Date: Wed, 11 Apr 2001 17:46:22 +0100 From: "Oliver Elphick" X-Archive-Number: 200104/160 X-Sequence-Number: 2508 This is a multipart MIME message. --==_Exmh_3126653160 Content-Type: text/plain; charset=us-ascii Kovacs Zoltan wrote: >> Use the "serial" column type. >Unfortunately it's not what I expect. Assume that I have an arbitrary >"SELECT expr1 as column1, expr2 as column2, ..." which gives > >column1 | column2 | ... >--------+---------+- ... >......data.............. >........................ > >I would like to get the same result with the only plus column row_no: > >row_no | column1 | column2 | ... >-------+---------+---------+- ... > 1 | ......data.............. > 2 | ........................ >................................. > >with a new SELECT statement: "SELECT ?????, expr1 as column1, expr2 as >column2, ...". What to write instead of ?????? Here is a method which is fairly cumbersome, but will do what you want. (Whether what you want is useful, is another matter. The row numbers have no meaning except to delineate which row is printed after which; they bear no relation to their order in the table.) Create the C code shown in the attachment. Compile it (the example shown is for Linux, see the programmer's manual for how to do it on other systems): gcc -fpic -c rowno.c gcc -shared -o rowno.so rowno.o In the database, create functions as shown (remember to change the directory from /tmp!): CREATE FUNCTION reset_row() RETURNS int4 AS '/tmp/rowno.so' LANGUAGE 'C'; CREATE FUNCTION row_no() RETURNS int4 AS '/tmp/rowno.so' LANGUAGE 'C'; Now you can use the function: bray=# select row_no() as row,id,name from person; row | id | name ------+--------+------------------------------------------------------- 1 | 100001 | Mr Graham Love (Director) 2 | 100002 | AILEEN BROWN ... but you have to do this in between queries: bray=# select reset_row(); because the numbers don't reset themselves: bray=# select row_no() as row,id,name from person; row | id | name -------+--------+------------------------------------------------------- 6015 | 100001 | Mr Graham Love (Director) 6016 | 100002 | AILEEN BROWN ... --==_Exmh_3126653160 Content-Type: text/x-csrc ; name="rowno.c"; charset=us-ascii Content-Description: rowno.c Content-Disposition: attachment; filename="rowno.c" #include "postgres.h" #include "fmgr.h" static int32 row = 0; PG_FUNCTION_INFO_V1(row_no); Datum row_no() { row += 1; PG_RETURN_INT32(row); } PG_FUNCTION_INFO_V1(reset_row); Datum reset_row() { row = 0; PG_RETURN_INT32(row); } --==_Exmh_3126653160 Content-Type: text/plain; charset=us-ascii Oliver Elphick Oliver.Elphick@lfix.co.uk Isle of Wight http://www.lfix.co.uk/oliver PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47 6B 7E 39 CC 56 E4 C1 47 GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C ======================================== "Is any one of you in trouble? He should pray. Is anyone happy? Let him sing songs of praise. Is any one of you sick? He should call the elders of the church to pray over him...The prayer of a righteous man is powerful and effective." James 5:13,14,16 --==_Exmh_3126653160--