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 C73A89DC992 for ; Sat, 17 Dec 2005 20:26:13 -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 08771-06 for ; Sat, 17 Dec 2005 20:26:15 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id AE95F9DC88F for ; Sat, 17 Dec 2005 20:26:10 -0400 (AST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id jBI0QB2K091774 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sat, 17 Dec 2005 17:26:13 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.4/8.13.4) with ESMTP id jBI0QAN9004936; Sat, 17 Dec 2005 17:26:10 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.4/8.13.4/Submit) id jBI0QA4q004935; Sat, 17 Dec 2005 17:26:10 -0700 (MST) (envelope-from mfuhr) Date: Sat, 17 Dec 2005 17:26:10 -0700 From: Michael Fuhr To: Michael Avila Cc: SQL PostgreSQL MailList Subject: Re: Need SQL Help Finding Current Status of members Message-ID: <20051218002610.GA4832@winnie.fuhr.org> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.025 required=5 tests=[AWL=0.025] X-Spam-Score: 0.025 X-Spam-Level: X-Archive-Number: 200512/146 X-Sequence-Number: 23688 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