Re: Expression on an Expression alias

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Mike Ellsworth" <younicycle(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Expression on an Expression alias
Date: 2008-04-18 20:31:57
Message-ID: 23531.1208550717@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

"Mike Ellsworth" <younicycle(at)gmail(dot)com> writes:
> The *effect* I'd like is to 'reuse' FV1, ~

> SELECT
> fv_test.acct_val AS acct_val,
> fv_test.time AS time,
> fv_test.i AS interest_rate,
> FV(fv_test.acct_val,fv_test.i,fv_test.time) AS FV1,
> FV1 *2 AS FV2,
> FV1 *3 AS FV3
> FROM "hrcommu"."fv_test"

This is flat out invalid according to the SQL spec. The SELECT
expressions are notionally computed in parallel and so can't refer
to each other.

You can do something vaguely like what you want with nested
SELECTs:

SELECT
FV1,
FV1 *2 AS FV2,
FV1 *3 AS FV3
FROM
(SELECT
FV(fv_test.acct_val,fv_test.i,fv_test.time) AS FV1
FROM "hrcommu"."fv_test"
) ss;

although how much advantage there is is debatable.
(In particular, this is very likely to still call fv()
three times per row.)

regards, tom lane

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Mike Ellsworth 2008-04-18 20:42:18 Re: Expression on an Expression alias
Previous Message Mike Ellsworth 2008-04-18 20:21:42 Expression on an Expression alias