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 1CEAF9DCA06 for ; Fri, 16 Dec 2005 09:45:47 -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 24691-03 for ; Fri, 16 Dec 2005 09:45:49 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from smtp106.sbc.mail.re2.yahoo.com (smtp106.sbc.mail.re2.yahoo.com [68.142.229.99]) by postgresql.org (Postfix) with SMTP id CC1A49DCA30 for ; Fri, 16 Dec 2005 09:45:42 -0400 (AST) Received: (qmail 83099 invoked from network); 16 Dec 2005 13:45:46 -0000 Received: from unknown (HELO epox2800) (michael.avila.1@sbcglobal.net@70.228.56.50 with login) by smtp106.sbc.mail.re2.yahoo.com with SMTP; 16 Dec 2005 13:45:45 -0000 From: "Michael Avila" To: "SQL PostgreSQL MailList" Subject: Re: Need SQL Help Finding Current Status of members Date: Fri, 16 Dec 2005 08:46:03 -0500 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-15" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.6604 (9.0.2911.0) In-Reply-To: <43A29304.9050801@anpe.fr> Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=2.099 required=5 tests=[AWL=-0.700, DNS_FROM_RFC_ABUSE=0.479, DNS_FROM_RFC_POST=1.44, DNS_FROM_RFC_WHOIS=0.879] X-Spam-Score: 2.099 X-Spam-Level: ** X-Archive-Number: 200512/140 X-Sequence-Number: 23682 Interesting. I think I understand that. I have never worked with a SELECT within a SELECT (I think that is called a subquery). I am guessing that it works its way through the member status records until the latest date "floats" to the top (nothing is > than it). Will that be a problem performance-wise if there are thousands of records? Thanks for the help. Mike > -----Original Message----- > From: pgsql-sql-owner@postgresql.org > [mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Patrick JACQUOT > Sent: Friday, December 16, 2005 5:12 AM > Cc: SQL PostgreSQL MailList > Subject: Re: [SQL] Need SQL Help Finding Current Status of members > > > 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) > > ---------------------------(end of broadcast)--------------------------- > TIP 5: don't forget to increase your free space map settings