From: Richard Huxton <dev@archonet.com>
To: Michael Avila <Michael.Avila.1@sbcglobal.net>
Cc: SQL PostgreSQL MailList <pgsql-sql@postgresql.org>
Subject: Re: Need SQL Help Finding Current Status of members
Date: Fri, 16 Dec 2005 09:24:22 +0000
Message-ID: <43A287C6.2090501@archonet.com> (raw)
In-Reply-To: <NBBBLNPHAMCFENDFHIDCGENBEGAA.Michael.Avila.1@sbcglobal.net>
References: <NBBBLNPHAMCFENDFHIDCGENBEGAA.Michael.Avila.1@sbcglobal.net>
Michael Avila wrote:
> I have a table which keeps track of the status of members. In the table is
>
>
> member_id int(8)
> status_code char(1)
> status_date date
>
> KEY member_id (member_id,status_code,status_date)
>
>
> Each member can have multiple records because a record is added each time
> the status changes but the old record is kept for history.
>
> What I want to do is find the latest status for each member.
Michael Fuhr has already described on solution, but if you can alter the
table definition then there might be a neater solution.
Replace "status_date" with "status_expires" and make it a "timestamp
with time zone". Set the expiry to 'infinity' for the current record and
you then have a simple select to find the most recent.
If you regularly want to find which record was active on a particular
time you'll want two columns: valid_from and valid_to. This makes it
much easier to find a row for a specific date.
--
Richard Huxton
Archonet Ltd
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: dev@archonet.com, Michael.Avila.1@sbcglobal.net
Subject: Re: Need SQL Help Finding Current Status of members
In-Reply-To: <43A287C6.2090501@archonet.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox