Unix Technical Forum

selecting un-ordered rows

This is a discussion on selecting un-ordered rows within the pgsql Novice forums, part of the PostgreSQL category; --> Hello, have enyone idea, how to select all rows, preceeding 'mary' (first two rows, but not LIMIT 2) name ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Novice

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 09:52 PM
=?ISO-8859-2?Q?Du=B9an_PE=A9L?=
 
Posts: n/a
Default selecting un-ordered rows

Hello,

have enyone idea, how to select all rows, preceeding 'mary'
(first two rows, but not LIMIT 2)

name date
albert 2006-05-17
john 2006-05-17
mary 2006-05-17
benjamin 2006-05-17
carl 2006-05-17
alice 2006-05-17

thanks for help

Dan

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 09:53 PM
Oscar Rodriguez Fonseca
 
Posts: n/a
Default Re: selecting un-ordered rows

El día Tue, 16 May 2006 13:34:33 +0200
Du¨an PE¦L <pesl@asprkyjov.cz> escribió:

> have enyone idea, how to select all rows, preceeding 'mary'
> (first two rows, but not LIMIT 2)
>
> name date
> albert 2006-05-17
> john 2006-05-17
> mary 2006-05-17
> benjamin 2006-05-17
> carl 2006-05-17
> alice 2006-05-17


You are implying that data is pre-ordered within the database, which IMHO is
incorrect in any relational database. If "un-ordered" means for you that is
ordered by date of insertion you might try adding a timestamp or serial
column to your table and perform a select like:

SELECT * FROM table ORDER BY tstamp LIMIT 2

If you do not want to add another column, you might try ordering by
oid "SELECT * FROM table ORDER BY oid LIMIT 2", provided you created
your table with oids (which I think is the default setting) but the
result is not guaranteed for a long lived database because oids can
roll-over.

You may want to have a look at:
http://www.postgresql.org/docs/8.1/s...ies-order.html
http://www.postgresql.org/docs/8.1/s...ies-limit.html

If you do not want to use limit you can use a WHERE clause instead. E.g.

SELECT * FROM table WHERE tstamp < (SELECT tstamp FROM table WHERE name = 'Mary')

Regards,

--
Oscar

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:03 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com