Re: Subselect Question

From: Richard Huxton <dev(at)archonet(dot)com>
To: Alex P <alex(at)meerkatsoft(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Subselect Question
Date: 2004-11-02 09:27:09
Message-ID: 418752ED.3000809@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Alex P wrote:
> Hi,
>
> when creating a query with a subselect
>
> SELECT name, (SELECT max(pop) FROM cities WHERE cities.state =
> states.name) AS max_pop
> FROM states;
>
> then it is not possible to sort after max_pop or use max_pop in a
> function or a CASE.

Here max_pop is naming the whole subselect. How about something like:

SELECT name, max_pop
FROM
states,
(SELECT state AS target_state, max(pop) AS max_pop FROM cities) AS pops
WHERE
states.name = pops.target_state
;

--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2004-11-02 09:32:17 Re: SQL Agreate Functions
Previous Message Sim Zacks 2004-11-02 09:05:38 Re: Subselect Question