Re: how to get row number in select query

From: "Oliveiros d'Azevedo Cristina" <oliveiros(dot)cristina(at)marktest(dot)pt>
To: <emilu(at)encs(dot)concordia(dot)ca>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: how to get row number in select query
Date: 2011-01-26 17:02:33
Message-ID: D25246ED495942DD8E5777F5F9439DD0@marktestcr.marktest.pt
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

If it is to order in ascendent fashion by, say, lname,
one possibility would be

SELECT COUNT(b.*) as row_number, a.lname,a.gname
FROM "Table1" a, "Table2" b
WHERE a.lname >= b.lname
GROUP BY a.lname,a.gname
ORDER BY row_number

If you want to order by gname just change the WHERE clause accordingly

N.B. : This works as long as there is no repetition on the column you use to
order.
If there is, we'll need a way to tie break. What is your specific case?

Also, note that this method is time consuming, and would work only for
relatively small tables.
AFAIK, version 8.3 doesn't have any "non-standard SQL" way to get a row
number, but it is possible that something like that has been introduced in
later versions...

Best,
Oliveiros

----- Original Message -----
From: "Emi Lu" <emilu(at)encs(dot)concordia(dot)ca>
To: <pgsql-sql(at)postgresql(dot)org>
Sent: Wednesday, January 26, 2011 4:11 PM
Subject: [SQL] how to get row number in select query

> Good morning,
>
> For postgresql 8.3, what is the system method/key word to get row number
> please?
>
> E.g.,
>
> ==================
> lname1 gname1
> lname2 gname2
> lname3 gname3
> ......
>
> I'd like to get
>
> 1 lname1 gname1
> 2 lname2 gname2
> 3 lname3 gname3
>
> ... ...
>
> Something like
> select row_number?, lname, gname from Table1;
>
> Thanks a lot!
>
>
> --
> Lu Ying
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message manuel antonio ochoa 2011-01-26 17:07:05 Compare the resulta of a count sql into bash
Previous Message Oliveiros d'Azevedo Cristina 2011-01-26 16:40:45 Re: how to get row number in select query