SQL - finding next date - Mailing list pgsql-general

From Raymond O'Donnell
Subject SQL - finding next date
Date
Msg-id 461D3997.6070508@iol.ie
Whole thread Raw
Responses Re: SQL - finding next date  ("Jeffrey Melloy" <jmelloy@gmail.com>)
Re: SQL - finding next date  (SCassidy@overlandstorage.com)
Re: SQL - finding next date  ("Chris Fischer" <Chris.Fischer@channeladvisor.com>)
Re: SQL - finding next date  (Jon Sime <jsime@mediamatters.org>)
Re: SQL - finding next date  ("Jeffrey Melloy" <melloyj@alliancetechnologies.net>)
List pgsql-general
Hi all,

This is probably a very simple one, but I just can't see the answer and
it's driving me nuts. I have a table holding details of academic terms,
and I need an SQL query such that for any given term I want to find the
next term by starting date (or just NULL if there isn't one).

Here's the table -

   CREATE TABLE terms
   (
     term_id serial NOT NULL,
     term_name character varying(40) NOT NULL,
     term_starts date NOT NULL,
     term_ends date NOT NULL,
     .....
   )

- so, supposing I have the following data -

    term_id |  term_name  | term_starts | ...
   ---------+-------------+-------------+--
          1 | Spring 2007 | 2007-01-10  | ...
          2 | Autumn 2007 | 2007-09-01  | ...
          6 | Spring 2008 | 2008-01-06  | ...

- then for term '1' I'd like to return '2', for term '2' I'd like to
return '6', and so on.

The closest I've got is getting ALL terms that start after a given one,
but I run into trouble after that....any help will be appreciated!

Thanks in advance,

Ray.

---------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod@iol.ie
---------------------------------------------------------------

pgsql-general by date:

Previous
From: "John D. Burger"
Date:
Subject: Re: Dumping part (not all) of the data in a database...methods?
Next
From: Tom Lane
Date:
Subject: Re: Dumping part (not all) of the data in a database...methods?