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: (view raw, whole thread or download thread mbox)
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;

	Holger Klawitter
Holger Klawitter                                     +49 (0)251 484 0637

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-2017 The PostgreSQL Global Development Group