pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas F.O'Connell <tfo@sitening.com>
To: Andrew Ward <adward555@yahoo.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: SQL confusion
Date: Tue, 12 Oct 2004 12:01:06 -0500
Message-ID: <4E690A87-1C70-11D9-975B-000D93AE0944@sitening.com> (raw)
In-Reply-To: <20041009203945.33114.qmail@web51609.mail.yahoo.com>
References: <20041009203945.33114.qmail@web51609.mail.yahoo.com>

This is untested, but it might be enough to get you started:

SELECT namecounter
FROM name n
WHERE NOT EXISTS (
	SELECT 1
	FROM name
	WHERE hh > 0
	AND famnu = n.famnu
)
GROUP BY famnu
HAVING birthdate = min( birthdate );

What I'm trying to do here is grab all families that don't have a head 
of household, group them by family, and get only the namecounter 
corresponding to the minimum birthdate for that family.

If I recall, I've had some trouble using HAVING with min/max in ways 
that seem intuitive to me, but this might help get you started.

-tfo

On Oct 9, 2004, at 3:39 PM, Andrew Ward wrote:

> 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
>
> ---------------------------(end of 
> broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
>                http://www.postgresql.org/docs/faqs/FAQ.html




view thread (4+ messages)

Message-ID: <4E690A87-1C70-11D9-975B-000D93AE0944@sitening.com>
Permalink:  ../4E690A87-1C70-11D9-975B-000D93AE0944@sitening.com/
Also on:    postgresql.org/message-id/4E690A87-1C70-11D9-975B-000D93AE0944@sitening.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tfo@sitening.com, adward555@yahoo.com
  Subject: Re: SQL confusion
  In-Reply-To: <4E690A87-1C70-11D9-975B-000D93AE0944@sitening.com>
  References: <20041009203945.33114.qmail@web51609.mail.yahoo.com> <4E690A87-1C70-11D9-975B-000D93AE0944@sitening.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox