From: | Bruno Wolff III <bruno(at)wolff(dot)to> |
---|---|
To: | Marco Lazzeri <marcomail(at)noze(dot)it> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Referencing subselect returned value |
Date: | 2004-01-21 18:10:59 |
Message-ID: | 20040121181059.GA11684@wolff.to |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Tue, Jan 20, 2004 at 21:21:56 +0100,
Marco Lazzeri <marcomail(at)noze(dot)it> wrote:
> I'd like to reference values returned by a subselect in the same SELECT
> query.
>
> Example:
>
> SELECT id,
> ( SELECT COUNT(*) FROM second ) AS value_to_reference,
> ( value_to_reference + 1 ) AS value_that_uses_referenced_one
> FROM first;
>
> Any suggestions?
If you want to use a subselect repeatedly, you can either repeat it or
join it. Doing a join will normally be better.
Repeated select example:
SELECT id,
( SELECT COUNT(*) FROM second) AS value_to_reference,
( SELECT COUNT(*) FROM second)+1 AS value_to_reference_one,
FROM first;
Join example:
SELECT id,
cnt AS value_to_reference,
cnt+1 AS value_reference_one,
FROM first, (select COUNT(*) AS cnt from second) AS third;
From | Date | Subject | |
---|---|---|---|
Next Message | Rick Gigger | 2004-01-21 18:23:40 | Re: embedded/"serverless" (Re: serverless postgresql) |
Previous Message | lnd | 2004-01-21 17:24:43 | Re: tablespaces a priority for 7.5? |