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 1C0D79DCD25 for ; Fri, 16 Dec 2005 06:24:10 -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 75839-07 for ; Fri, 16 Dec 2005 06:24:11 -0400 (AST) X-Greylist: delayed 00:11:25.761059 by SQLgrey- Received: from smtp3.anpe.fr (unknown [80.124.143.18]) by postgresql.org (Postfix) with ESMTP id 9BAAF9DCD39 for ; Fri, 16 Dec 2005 06:24:07 -0400 (AST) Received: from homere03.anpe.fr (localhost.localdomain [127.0.0.1]) by smtp3.anpe.fr (antivirus) with ESMTP id 82B1873D5A for ; Fri, 16 Dec 2005 11:12:41 +0100 (CET) Received: from HOMERE01.anpe.fr ([10.0.6.136]) by homere03.anpe.fr with Microsoft SMTPSVC(6.0.3790.1830); Fri, 16 Dec 2005 11:12:04 +0100 Received: from [10.0.32.170] ([10.0.32.170]) by HOMERE01.anpe.fr with Microsoft SMTPSVC(6.0.3790.1830); Fri, 16 Dec 2005 11:12:04 +0100 Message-ID: <43A29304.9050801@anpe.fr> Date: Fri, 16 Dec 2005 11:12:20 +0100 From: Patrick JACQUOT User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 Cc: SQL PostgreSQL MailList Subject: Re: Need SQL Help Finding Current Status of members References: <43A287C6.2090501@archonet.com> In-Reply-To: <43A287C6.2090501@archonet.com> Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 16 Dec 2005 10:12:04.0376 (UTC) FILETIME=[29BC5180:01C60229] X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.095 required=5 tests=[AWL=-0.095, MISSING_HEADERS=0.189] X-Spam-Score: 0.095 X-Spam-Level: X-Archive-Number: 200512/139 X-Sequence-Number: 23681 Richard Huxton wrote: > 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. There is a standard way : Select * from memberstatus A where not exists (select * from emberstatus B where B.member_id=A.member_id and B.status_date >A.status_date)