Thread: enumerating rows

enumerating rows

From
Kovacs Zoltan
Date:
Maybe a trivial question, maybe it's foreign from SQL, I'dont know...
How to add a column which stands for the row number in each row of the
result? E.g.:

row_no | column1 | column2 | ...
-------+---------+---------+ ...    1 | datum11 | datum12 | ...    2 | datum21 | datum22 | ...  ... |     ... |     ...
|...
 

I didn't find anything in the docs.

TIA, Zoltan

--                         Kov\'acs, Zolt\'an                        kovacsz@pc10.radnoti-szeged.sulinet.hu
          http://www.math.u-szeged.hu/~kovzol                        ftp://pc10.radnoti-szeged.sulinet.hu/home/kovacsz
 



Re: enumerating rows

From
"Poul L. Christiansen"
Date:
Use the "serial" column type.

create table myTable (row_no serial,column1 varchar(10),column2
varchar(20));

HTH,
Poul L. Christiansen

On Wed, 11 Apr 2001, Kovacs Zoltan wrote:

> Maybe a trivial question, maybe it's foreign from SQL, I'dont know...
> How to add a column which stands for the row number in each row of the
> result? E.g.:
> 
> row_no | column1 | column2 | ...
> -------+---------+---------+ ...
>      1 | datum11 | datum12 | ...
>      2 | datum21 | datum22 | ...
>    ... |     ... |     ... | ...
> 
> I didn't find anything in the docs.
> 
> TIA, Zoltan
> 
> -- 
>                          Kov\'acs, Zolt\'an
>                          kovacsz@pc10.radnoti-szeged.sulinet.hu
>                          http://www.math.u-szeged.hu/~kovzol
>                          ftp://pc10.radnoti-szeged.sulinet.hu/home/kovacsz
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
> 
> http://www.postgresql.org/search.mpl
> 



Re: enumerating rows

From
Kovacs Zoltan
Date:
> 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 ??????

TIA, Zoltan



RE: enumerating rows

From
"Koen Antonissen"
Date:
I was searching for the same thing, I couldn't found it though :(

-----Original Message-----
From: Kovacs Zoltan [mailto:kovacsz@pc10.radnoti-szeged.sulinet.hu]
Sent: woensdag 11 april 2001 16:37
To: Poul L. Christiansen
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] enumerating rows


> 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 ??????

TIA, Zoltan


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/users-lounge/docs/faq.html


Re: enumerating rows

From
"Oliver Elphick"
Date:
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
...

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

Attachment

Re: enumerating rows

From
Kovacs Zoltan
Date:
> 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.)
Thanks, Oliver! Are you sure there is no other (more
convenient) solution? I don't think this is a part of the SQL standard but
it could be a PostgreSQL extension. Hm?

Regards, Zoltan



Re: enumerating rows

From
"Josh Berkus"
Date:
Kovacs, Oliver,
First, thank you Oliver for the nice C program for this purpose.  If
there doesn't turn out to be another method, it shoudl og in the
postgresql.org site.
However, Postgresql does have an internal row count for query results.
Otherwise LIMIT and OFFSET would not work.  It seems like there should
be some way to access this internal row count.
                -Josh

______AGLIO DATABASE SOLUTIONS___________________________                                      Josh Berkus Complete
informationtechnology      josh@agliodbs.com  and data management solutions       (415) 565-7293 for law firms, small
businesses       fax 621-2533   and non-profit organizations.      San Francisco
 


Re: enumerating rows

From
"Oliver Elphick"
Date:
Kovacs Zoltan wrote: >> Here is a method which is fairly cumbersome, but will do what you want. >> (Whether what you
wantis 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.) >Thanks, Oliver! Are you sure there is no other (more
>convenient)solution? I don't think this is a part of the SQL standard but >it could be a PostgreSQL extension. Hm?
 

I believe Oracle has an inbuilt feature to do this; I don't know about
any other database.  Nor do I know if any other databases can accommodate
user-defined functions.

But what is the actual use of this feature?  Why do you need it?

-- 
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
singsongs of praise. Is any one     of you sick? He should call the elders of the church     to pray over him...The
prayerof a righteous man is     powerful and effective."         James 5:13,14,16 
 




Re: enumerating rows

From
Joel Burton
Date:
On Wed, 11 Apr 2001, Kovacs Zoltan wrote:

> > 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.)
> Thanks, Oliver! Are you sure there is no other (more
> convenient) solution? I don't think this is a part of the SQL standard but
> it could be a PostgreSQL extension. Hm?

If you don't need the numbers in PostgreSQL, but in the output, could you
pipe your query results through `cat -b`, which will add line numbers?

-- 
Joel Burton   <jburton@scw.org>
Director of Information Systems, Support Center of Washington



Re: enumerating rows

From
Peter Eisentraut
Date:
Kovacs Zoltan writes:

> Maybe a trivial question, maybe it's foreign from SQL, I'dont know...
> How to add a column which stands for the row number in each row of the
> result? E.g.:
>
> row_no | column1 | column2 | ...
> -------+---------+---------+ ...
>      1 | datum11 | datum12 | ...
>      2 | datum21 | datum22 | ...
>    ... |     ... |     ... | ...
>
> I didn't find anything in the docs.

Your client can do that.  When you fetch the results you have to have some
sort of loop anyway, so you have (or keep) a counter there.  There's no
use of the server generating this information, because the numbering is
implied by the order in which the rows are sent.

-- 
Peter Eisentraut      peter_e@gmx.net       http://yi.org/peter-e/



Re: enumerating rows

From
"Luis C. Ferreira"
Date:
Try this....

CREATE SEQUENCE just_a_seq;
Select nextval('just_a_seq') as row_no, * from pg_tables ;
drop SEQUENCE just_a_seq;

>
> row_no | column1 | column2 | ...
> -------+---------+---------+ ...
>      1 | datum11 | datum12 | ...
>      2 | datum21 | datum22 | ...
>    ... |     ... |     ... | ...
>
> I didn't find anything in the docs.
>
> TIA, Zoltan
>






Re: enumerating rows

From
Kovacs Zoltan
Date:
> Try this....
> 
> CREATE SEQUENCE just_a_seq;
> Select nextval('just_a_seq') as row_no, * from pg_tables ;
> drop SEQUENCE just_a_seq;
Wow! Great idea! :-)

In fact I need row numbering in browsing and printing invoices. They
should be read on various platforms (Windows clients, generated HTML and
printed reports made by Windows) and it would be good to unify the
handling of enumeration. Of course, all clients can enumerate the result
somehow, but it's hard to maintain the separate codes.

Regards, Zoltan

--                         Kov\'acs, Zolt\'an                        kovacsz@pc10.radnoti-szeged.sulinet.hu
          http://www.math.u-szeged.hu/~kovzol                        ftp://pc10.radnoti-szeged.sulinet.hu/home/kovacsz