Re: [HACKERS] correlated subquery

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: sszabo(at)bigpanda(dot)com
Cc: pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: [HACKERS] correlated subquery
Date: 1999-12-30 00:44:07
Message-ID: 199912300044.TAA16571@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

>
> >Is this a good example of a required correlated subquery:
> >
> > SELECT f1.firstname, f1.lastname, f1.age
> > FROM friends f1
> > WHERE age = (
> > SELECT MAX(age)
> > FROM friends f2
> > WHERE f1.state = f2.state
> > )
> > ORDER BY firstname, lastname
> >
> >It finds the oldest person in each state. HAVING can't do that, right?
>
> I'm assuming that this is for the book... If so, you might want to also
> note that this query can return more people than there are states if
> multiple people in the same state have the maximum age for that state.
>
> I'm not sure how deeply you are going into this, but getting only one
> person per state looks like it might be fairly painful... You might be
> able cheat if there was only one field besides age and state in the output
> using group by and an aggregate.

Yikes, that would be painful. Good point. Fortunately, the data has
only one max person per state.

--
Bruce Momjian | http://www.op.net/~candle
maillist(at)candle(dot)pha(dot)pa(dot)us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 1999-12-30 06:04:03 Source code format vote
Previous Message sszabo 1999-12-29 23:59:18 Re: [HACKERS] correlated subquery