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 5EBFA32A088 for ; Sat, 9 Oct 2004 21:39:51 +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 49306-10 for ; Sat, 9 Oct 2004 20:39:46 +0000 (GMT) Received: from web51609.mail.yahoo.com (web51609.mail.yahoo.com [206.190.38.214]) by svr1.postgresql.org (Postfix) with SMTP id 00F0532A06C for ; Sat, 9 Oct 2004 21:39:46 +0100 (BST) Message-ID: <20041009203945.33114.qmail@web51609.mail.yahoo.com> Received: from [68.185.205.40] by web51609.mail.yahoo.com via HTTP; Sat, 09 Oct 2004 13:39:45 PDT Date: Sat, 9 Oct 2004 13:39:45 -0700 (PDT) From: Andrew Ward Subject: SQL confusion To: pgsql-sql@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.9 tagged_above=0.0 required=5.0 tests=FROM_ENDS_IN_NUMS, MAILTO_TO_SPAM_ADDR X-Spam-Level: * X-Archive-Number: 200410/59 X-Sequence-Number: 19162 I'm trying to figure out how to do a particular query, and I'm beating my head against a wall. Here's my situation: I'm running postgres 7.3.2 on linux, and making my requests from Perl scripts using DBD::Pg. My table structure is as follows (irrelevant cols removed) 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. I can pull the list with hh>0 easily enough, but I'm not sure how to pull out the others. I realize that this could be done through some looping in the Perl script, but I'd like to avoid pulling the whole list into memory in case the list gets long. My preference is to just handle one record at a time in Perl if possible. Help? Andrew Ward adward55@yahoo.com __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com