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 E0DA59DCCB9 for ; Thu, 15 Dec 2005 22:58:53 -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 38283-07 for ; Thu, 15 Dec 2005 22:58:58 -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 A43659DCC81 for ; Thu, 15 Dec 2005 22:58:51 -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 jBG2wqjG089031 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 15 Dec 2005 19:58:55 -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 jBG2wq2w048769; Thu, 15 Dec 2005 19:58:52 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.4/8.13.4/Submit) id jBG2wqbC048768; Thu, 15 Dec 2005 19:58:52 -0700 (MST) (envelope-from mfuhr) Date: Thu, 15 Dec 2005 19:58:52 -0700 From: Michael Fuhr To: Michael Avila Cc: SQL PostgreSQL MailList Subject: Re: Need SQL Help Finding Current Status of members Message-ID: <20051216025852.GA48639@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.013 required=5 tests=[AWL=0.013] X-Spam-Score: 0.013 X-Spam-Level: X-Archive-Number: 200512/136 X-Sequence-Number: 23678 On Thu, Dec 15, 2005 at 08:31:09PM -0500, Michael Avila wrote: > What I want to do is find the latest status for each member. Actually I want > to find all those with an status of "A". But it must be the current (latest) > status. How do I find the most current date for each member in a pile of > many records for many members with many status settings with one SQL > statement? Suppose you have this table: SELECT * FROM memberstatus; member_id | status_code | status_date -----------+-------------+------------- 1 | a | 2005-01-01 2 | x | 2005-01-01 3 | x | 2005-01-01 4 | x | 2005-01-01 1 | x | 2005-12-15 2 | a | 2005-12-15 3 | y | 2005-12-15 4 | a | 2005-12-15 (8 rows) Let's order the data so all of a member's records are shown together, with the latest one first: SELECT * FROM memberstatus ORDER BY member_id, status_date DESC; member_id | status_code | status_date -----------+-------------+------------- 1 | x | 2005-12-15 1 | a | 2005-01-01 2 | a | 2005-12-15 2 | x | 2005-01-01 3 | y | 2005-12-15 3 | x | 2005-01-01 4 | a | 2005-12-15 4 | x | 2005-01-01 (8 rows) One way to get only the first record for each member is to use PostgreSQL's nonstandard DISTINCT ON construct: SELECT DISTINCT ON (member_id) * FROM memberstatus ORDER BY member_id, status_date DESC; member_id | status_code | status_date -----------+-------------+------------- 1 | x | 2005-12-15 2 | a | 2005-12-15 3 | y | 2005-12-15 4 | a | 2005-12-15 (4 rows) We could put the above in a subquery and restrict the output to the records we want: SELECT * FROM ( SELECT DISTINCT ON (member_id) * FROM memberstatus ORDER BY member_id, status_date DESC ) AS s WHERE status_code = 'a' ORDER BY member_id; member_id | status_code | status_date -----------+-------------+------------- 2 | a | 2005-12-15 4 | a | 2005-12-15 (2 rows) This isn't the only way; search the archives for alternatives. -- Michael Fuhr