Skip site navigation (1) Skip section navigation (2)

finding predecessor

From: Holger Klawitter <holger(at)klawitter(dot)de>
To: PostgreSQL-general <pgsql-general(at)postgreSQL(dot)org>
Subject: finding predecessor
Date: 2000-01-27 09:22:51
Message-ID: 38900E6B.21233118@klawitter.de (view raw or flat)
Thread:
Lists: pgsql-general
Hi there,

is there any convenient way to find the predecessor of a record given
a particular order of the database? 

The following select seems a little bit too expensive.

	create table tuples ( i int, j int, m int, k int );

	select * from tuples
	where i<i0
	  or ( i=i0 and ( j<j0
	    or ( j=j0 and ( k<k0 
	      or ( k=k0 and m<m0 )
            ))
          ))
	order by
	  i desc, j desc, k desc, m desc
	limit 1;

I would like to have something like:

	declare c cursor for
		select * from tuples
		order by i,j,k,m;
	fetch select *
		where i=i0 and j=j0 and k=k0 and m=m0 
	in c;
	fetch backward 1 in c;

Regards,
	Holger Klawitter
--
Holger Klawitter                                     +49 (0)251 484 0637
holger(at)klawitter(dot)de                             http://www.klawitter.de/



pgsql-general by date

Next:From: Jim MercerDate: 2000-01-27 09:31:17
Subject: finding last element of an index
Previous:From: Mark JewissDate: 2000-01-27 08:47:03
Subject: Re: [GENERAL] reverse sorting

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group