From: | Richard Poole <rp(at)guests(dot)deus(dot)net> |
---|---|
To: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: SQL confusion |
Date: | 2004-10-09 21:11:45 |
Message-ID: | 20041009211145.GA8572@guests.deus.net |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
On Sat, Oct 09, 2004 at 01:39:45PM -0700, Andrew Ward wrote:
>
> 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,
> );
This may not be the fastest query, but it should be quite comprehensible.
Lightly tested only on 7.4.5 (do you know why you're not using 7.4.x?).
SELECT namecounter -- and whatever other columns you need
FROM name outername -- alias so it's clear what the joins do
WHERE hh = 1 -- simple case: head of family
OR (
hh = 0 -- not a head
AND NOT EXISTS ( -- there is no head of this family
SELECT namecounter
FROM name in1
WHERE hh = 1 AND in1.famnu = outername.famnu)
AND birthdate = ( -- this person is as old as the oldest person
SELECT MIN(birthdate)
FROM name in2
WHERE in2.famnu = outername.famnu)
);
Richard
From | Date | Subject | |
---|---|---|---|
Next Message | Michael Fuhr | 2004-10-09 21:59:51 | Re: SQL confusion |
Previous Message | Andrew Ward | 2004-10-09 20:39:45 | SQL confusion |