pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
enumerating rows
12+ messages / 8 participants
[nested] [flat]

* enumerating rows
@ 2001-04-11 12:02 Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  2001-04-11 13:29 ` Re: enumerating rows Poul L. Christiansen <poulc@cs.auc.dk>
  2001-04-12 06:05 ` Re: enumerating rows Luis C. Ferreira <lferreira@ciudad.com.ar>
  2001-04-12 22:40 ` Re: enumerating rows Peter Eisentraut <peter_e@gmx.net>
  0 siblings, 3 replies; 12+ messages in thread

From: Kovacs Zoltan @ 2001-04-11 12:02 UTC (permalink / raw)
  To: pgsql-sql

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




^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: enumerating rows
  2001-04-11 12:02 enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
@ 2001-04-11 13:29 ` Poul L. Christiansen <poulc@cs.auc.dk>
  2001-04-11 14:37   ` Re: enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  2 siblings, 1 reply; 12+ messages in thread

From: Poul L. Christiansen @ 2001-04-11 13:29 UTC (permalink / raw)
  To: Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>; +Cc: pgsql-sql

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
> 




^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: enumerating rows
  2001-04-11 12:02 enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  2001-04-11 13:29 ` Re: enumerating rows Poul L. Christiansen <poulc@cs.auc.dk>
@ 2001-04-11 14:37   ` Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  2001-04-11 16:46     ` Re: enumerating rows Oliver Elphick <olly@lfix.co.uk>
  0 siblings, 1 reply; 12+ messages in thread

From: Kovacs Zoltan @ 2001-04-11 14:37 UTC (permalink / raw)
  To: Poul L. Christiansen <poulc@cs.auc.dk>; +Cc: pgsql-sql

> 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




^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: enumerating rows
  2001-04-11 12:02 enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  2001-04-11 13:29 ` Re: enumerating rows Poul L. Christiansen <poulc@cs.auc.dk>
  2001-04-11 14:37   ` Re: enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
@ 2001-04-11 16:46     ` Oliver Elphick <olly@lfix.co.uk>
  2001-04-11 16:43       ` Re: enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  0 siblings, 1 reply; 12+ messages in thread

From: Oliver Elphick @ 2001-04-11 16:46 UTC (permalink / raw)
  To: Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>; +Cc: Koen Antonissen <Koen@Cee-Kay.net>; pgsql-sql

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 

Attachments:

rowno.c (text/x-csrc, 235B) download #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); }
^ permalink raw reply [nested|flat] 12+ messages in thread

* Re: enumerating rows
  2001-04-11 12:02 enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  2001-04-11 13:29 ` Re: enumerating rows Poul L. Christiansen <poulc@cs.auc.dk>
  2001-04-11 14:37   ` Re: enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  2001-04-11 16:46     ` Re: enumerating rows Oliver Elphick <olly@lfix.co.uk>
@ 2001-04-11 16:43       ` Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  2001-04-11 18:29         ` Re: enumerating rows Josh Berkus <josh@agliodbs.com>
  2001-04-11 20:08         ` Re: enumerating rows Oliver Elphick <olly@lfix.co.uk>
  2001-04-11 21:53         ` Re: enumerating rows Joel Burton <jburton@scw.org>
  0 siblings, 3 replies; 12+ messages in thread

From: Kovacs Zoltan @ 2001-04-11 16:43 UTC (permalink / raw)
  To: Oliver Elphick <olly@lfix.co.uk>; +Cc: Koen Antonissen <Koen@Cee-Kay.net>; pgsql-sql

> 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




^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: enumerating rows
  2001-04-11 12:02 enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  2001-04-11 13:29 ` Re: enumerating rows Poul L. Christiansen <poulc@cs.auc.dk>
  2001-04-11 14:37   ` Re: enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  2001-04-11 16:46     ` Re: enumerating rows Oliver Elphick <olly@lfix.co.uk>
  2001-04-11 16:43       ` Re: enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
@ 2001-04-11 18:29         ` Josh Berkus <josh@agliodbs.com>
  2 siblings, 0 replies; 12+ messages in thread

From: Josh Berkus @ 2001-04-11 18:29 UTC (permalink / raw)
  To: pgsql-sql

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 information technology      josh@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: enumerating rows
  2001-04-11 12:02 enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  2001-04-11 13:29 ` Re: enumerating rows Poul L. Christiansen <poulc@cs.auc.dk>
  2001-04-11 14:37   ` Re: enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  2001-04-11 16:46     ` Re: enumerating rows Oliver Elphick <olly@lfix.co.uk>
  2001-04-11 16:43       ` Re: enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
@ 2001-04-11 20:08         ` Oliver Elphick <olly@lfix.co.uk>
  2 siblings, 0 replies; 12+ messages in thread

From: Oliver Elphick @ 2001-04-11 20:08 UTC (permalink / raw)
  To: Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>; +Cc: Koen Antonissen <Koen@Cee-Kay.net>; pgsql-sql

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?

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





^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: enumerating rows
  2001-04-11 12:02 enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  2001-04-11 13:29 ` Re: enumerating rows Poul L. Christiansen <poulc@cs.auc.dk>
  2001-04-11 14:37   ` Re: enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  2001-04-11 16:46     ` Re: enumerating rows Oliver Elphick <olly@lfix.co.uk>
  2001-04-11 16:43       ` Re: enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
@ 2001-04-11 21:53         ` Joel Burton <jburton@scw.org>
  2 siblings, 0 replies; 12+ messages in thread

From: Joel Burton @ 2001-04-11 21:53 UTC (permalink / raw)
  To: Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>; +Cc: Oliver Elphick <olly@lfix.co.uk>; Koen Antonissen <Koen@Cee-Kay.net>; pgsql-sql

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




^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: enumerating rows
  2001-04-11 12:02 enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
@ 2001-04-12 06:05 ` Luis C. Ferreira <lferreira@ciudad.com.ar>
  2 siblings, 0 replies; 12+ messages in thread

From: Luis C. Ferreira @ 2001-04-12 06:05 UTC (permalink / raw)
  To: pgsql-sql


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
>







^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: enumerating rows
  2001-04-11 12:02 enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
@ 2001-04-12 22:40 ` Peter Eisentraut <peter_e@gmx.net>
  2 siblings, 0 replies; 12+ messages in thread

From: Peter Eisentraut @ 2001-04-12 22:40 UTC (permalink / raw)
  To: Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>; +Cc: pgsql-sql

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/




^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* RE: enumerating rows
@ 2001-04-11 15:56 Koen Antonissen <Koen@Cee-Kay.net>
  0 siblings, 0 replies; 12+ messages in thread

From: Koen Antonissen @ 2001-04-11 15:56 UTC (permalink / raw)
  To: Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>; Poul L. Christiansen <poulc@cs.auc.dk>; +Cc: pgsql-sql

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



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: enumerating rows
@ 2001-04-14 08:14 Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
  0 siblings, 0 replies; 12+ messages in thread

From: Kovacs Zoltan @ 2001-04-14 08:14 UTC (permalink / raw)
  To: Luis Ferreira <lferreira@ciudad.com.ar>; +Cc: pgsql-sql

> 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




^ permalink  raw  reply  [nested|flat] 12+ messages in thread


end of thread, other threads:[~2001-04-14 08:14 UTC | newest]

Thread overview: 12+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-04-11 12:02 enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
2001-04-11 13:29 ` Poul L. Christiansen <poulc@cs.auc.dk>
2001-04-11 14:37   ` Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
2001-04-11 16:46     ` Oliver Elphick <olly@lfix.co.uk>
2001-04-11 16:43       ` Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>
2001-04-11 18:29         ` Josh Berkus <josh@agliodbs.com>
2001-04-11 20:08         ` Oliver Elphick <olly@lfix.co.uk>
2001-04-11 21:53         ` Joel Burton <jburton@scw.org>
2001-04-12 06:05 ` Luis C. Ferreira <lferreira@ciudad.com.ar>
2001-04-12 22:40 ` Peter Eisentraut <peter_e@gmx.net>
2001-04-11 15:56 RE: enumerating rows Koen Antonissen <Koen@Cee-Kay.net>
2001-04-14 08:14 Re: enumerating rows Kovacs Zoltan <kovacsz@pc10.radnoti-szeged.sulinet.hu>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox