X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id ED90D9DCC77 for ; Fri, 16 Dec 2005 05:25:25 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 68754-02 for ; Fri, 16 Dec 2005 05:25:27 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by postgresql.org (Postfix) with ESMTP id DBB9E9DC807 for ; Fri, 16 Dec 2005 05:25:23 -0400 (AST) Received: from mainbox.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 0705340DC4F; Fri, 16 Dec 2005 09:24:18 +0000 (GMT) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id BF5F915EA4; Fri, 16 Dec 2005 09:24:22 +0000 (GMT) Message-ID: <43A287C6.2090501@archonet.com> Date: Fri, 16 Dec 2005 09:24:22 +0000 From: Richard Huxton User-Agent: Thunderbird 1.5 (X11/20051025) MIME-Version: 1.0 To: Michael Avila Cc: SQL PostgreSQL MailList Subject: Re: Need SQL Help Finding Current Status of members References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200512/138 X-Sequence-Number: 23680 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