Probably simple answer

Started by Al Kirkusover 24 years ago2 messagesgeneral
Jump to latest
#1Al Kirkus
Al@dist102.k12.il.us

Hi all.

Can anyone tell me how to get a sequential row count field in the output of a query?

Say I want to query for all users in a table sorted by lastname and firstname.
I would like to include a column in my query called "rownum" which would uniquely identify the row in the order of the query results.

Like this:

rownum =1 lastname=jones, firstname=john
rownum=2 lastname=smith, firstname=john

etc.
I assume rownum should be some kind of function of expresion but I don't know what.

Something like:

Select ???? as rownum, lastname,firstname from users where xxx =xxx order by lastname, firsname.

#2Ian Harding
ianh@tpchd.org
In reply to: Al Kirkus (#1)
Re: Probably simple answer

I can't think of a simple answer. I think you would need some kind of programmatic solution like a client side program that would create a temporary table with a serial field in it, populate it with data based on your query, then return the data. The temp table would go away with the connection. A function could create the table but you would still have to query the resulting table since functions can't return query results directly yet.

Ian A. Harding
Programmer/Analyst II
Tacoma-Pierce County Health Department
(253) 798-3549
mailto: ianh@tpchd.org

"Al Kirkus" <Al@dist102.k12.il.us> 11/01/01 12:24PM >>>

Hi all.

Can anyone tell me how to get a sequential row count field in the output of a query?

Say I want to query for all users in a table sorted by lastname and firstname.
I would like to include a column in my query called "rownum" which would uniquely identify the row in the order of the query results.

Like this:

rownum =1 lastname=jones, firstname=john
rownum=2 lastname=smith, firstname=john

etc.
I assume rownum should be some kind of function of expresion but I don't know what.

Something like:

Select ???? as rownum, lastname,firstname from users where xxx =xxx order by lastname, firsname.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly