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 ED6619DC992 for ; Sat, 17 Dec 2005 20:34:20 -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 15032-09 for ; Sat, 17 Dec 2005 20:34:22 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from smtp110.sbc.mail.re2.yahoo.com (smtp110.sbc.mail.re2.yahoo.com [68.142.229.95]) by postgresql.org (Postfix) with SMTP id C28419DC88F for ; Sat, 17 Dec 2005 20:34:17 -0400 (AST) Received: (qmail 79470 invoked from network); 18 Dec 2005 00:34:20 -0000 Received: from unknown (HELO epox2800) (michael.avila.1@sbcglobal.net@70.228.56.50 with login) by smtp110.sbc.mail.re2.yahoo.com with SMTP; 18 Dec 2005 00:34:20 -0000 From: "Michael Avila" To: "SQL PostgreSQL MailList" Subject: Re: Need SQL Help Finding Current Status of members Date: Sat, 17 Dec 2005 19:34:22 -0500 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" 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: <20051218002610.GA4832@winnie.fuhr.org> 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.178 required=5 tests=[AWL=-0.620, DNS_FROM_RFC_ABUSE=0.479, DNS_FROM_RFC_POST=1.44, DNS_FROM_RFC_WHOIS=0.879] X-Spam-Score: 2.178 X-Spam-Level: ** X-Archive-Number: 200512/147 X-Sequence-Number: 23689 The table is not empty. I did some playing around with the SQL Statement but got no where. So I added a record status column that will be non-NULL when the status becomes history because a new status is added. Yeah, I cheated to make it easy!! LOL But it now works and that is what counts! Thanks for the help. Mike > -----Original Message----- > From: Michael Fuhr [mailto:mike@fuhr.org] > Sent: Saturday, December 17, 2005 7:26 PM > To: Michael Avila > Cc: SQL PostgreSQL MailList > Subject: Re: [SQL] Need SQL Help Finding Current Status of members > > > On Fri, Dec 16, 2005 at 07:44:46PM -0500, Michael Avila wrote: > > Just tried it and it returned nothing. > > > > > > 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) > > > > Why the WHERE NOT EXISTS? > > The query selects each row in memberstatus for which no other rows > (WHERE NOT EXISTS) with the same member_id have a later status_date; > in other words, the row(s) with the latest status_date for each > member_id. For example, given > > member_id | status_code | status_date > -----------+-------------+------------- > 1 | a | 2005-12-01 > 1 | b | 2005-12-02 > 1 | c | 2005-12-03 > 2 | x | 2005-12-11 > 2 | y | 2005-12-12 > 2 | z | 2005-12-13 > > the query should return > > member_id | status_code | status_date > -----------+-------------+------------- > 1 | c | 2005-12-03 > 2 | z | 2005-12-13 > > Offhand I can't think of why the query would return nothing unless > the table is empty, but maybe I'm overlooking something or making > unwarranted assumptions about the data. Can you post a sample data > set for which the query returns no rows? > > -- > Michael Fuhr