X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 2881232A7E5 for ; Sat, 9 Oct 2004 23:00:09 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 67325-03 for ; Sat, 9 Oct 2004 21:59:56 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 9C3E732A7BE for ; Sat, 9 Oct 2004 22:59:57 +0100 (BST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.1/8.13.1) with ESMTP id i99Lxp5E018084 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sat, 9 Oct 2004 15:59:54 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.1/8.13.1) with ESMTP id i99LxpJk016085; Sat, 9 Oct 2004 15:59:51 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.1/8.13.1/Submit) id i99Lxp6W016084; Sat, 9 Oct 2004 15:59:51 -0600 (MDT) (envelope-from mfuhr) Date: Sat, 9 Oct 2004 15:59:51 -0600 From: Michael Fuhr To: Andrew Ward Cc: pgsql-sql@postgresql.org Subject: Re: SQL confusion Message-ID: <20041009215951.GA15963@winnie.fuhr.org> References: <20041009203945.33114.qmail@web51609.mail.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20041009203945.33114.qmail@web51609.mail.yahoo.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/61 X-Sequence-Number: 19164 On Sat, Oct 09, 2004 at 01:39:45PM -0700, Andrew Ward wrote: > > CREATE TABLE name ( > namecounter integer NOT NULL, > firstmiddle character varying(64) NOT NULL, > lastname character varying(64) NOT NULL, > birthdate date, > hh smallint, > famnu integer, > ); > > Each row represents a person with a unique > namecounter. Families share a famnu, and usually one > person in a family is marked as head of household > (hh>0), with everyone else hh=0. However, there are a > few families with nobody marked as hh, and I'd like to > elect one by age. The query I'm trying to do is to > pull one person from each household, either the head > of household if available, or the eldest if not. I > want them sorted by last name, so I'd prefer to find > them all in one query, no matter how ugly and nested > it has to be. This should be close to what you need: SELECT DISTINCT ON (lastname, famnu) * FROM name ORDER BY lastname, famnu, COALESCE(hh, 0) DESC, birthdate; The order of the fields in the ORDER BY clause is important, so if you want to sort the results by some other criteria then you might need to do it with a subselect. I used COALESCE on the hh field because a descending sort puts NULL values ahead of non-NULL values -- without COALESCE a person with hh=1 won't be recognized as the head of household if another member of the family has hh=NULL. You might need to modify the query to handle NULL birthdates or to add other tiebreakers. -- Michael Fuhr http://www.fuhr.org/~mfuhr/