Re: SQL - finding next date

From: Jon Sime <jsime(at)mediamatters(dot)org>
To: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: SQL - finding next date
Date: 2007-04-11 20:15:13
Message-ID: 461D41D1.3080800@mediamatters.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Raymond O'Donnell wrote:
> 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 one approach given your table def.

select t.*,
( select term_id
from terms
where term_starts > t.term_ends
order by term_starts asc
limit 1
) as next_term_id
from terms t
order by t.term_starts asc;

-Jon

--
Senior Systems Developer
Media Matters for America
http://mediamatters.org/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2007-04-11 20:52:31 Re: Dumping part (not all) of the data in a database...methods?
Previous Message Raymond O'Donnell 2007-04-11 19:40:07 SQL - finding next date