pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Reuse previously calculated column in sql query?
3+ messages / 3 participants
[nested] [flat]

* Reuse previously calculated column in sql query?
@ 2004-10-20 12:50  Philippe Lang <philippe.lang@attiksystem.ch>
  0 siblings, 1 reply; 3+ messages in thread

From: Philippe Lang @ 2004-10-20 12:50 UTC (permalink / raw)
  To: pgsql-sql

Hello,

Is it possible to reuse a previously calculated column in a following
column, like:

SELECT
foo.val1 AS col1,
long_calculation(foo.val1) AS col2,
COL2 * 0.75 AS col3
FROM foo;

instead of writing:

SELECT
foo.val1 AS col1,
long_calculation(foo.val1) AS col2,
long_calculation(foo.val1) * 0.75 AS col3
FROM foo;


Philippe



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Reuse previously calculated column in sql query?
@ 2004-10-20 13:05  Rod Taylor <pg@rbt.ca>
  parent: Philippe Lang <philippe.lang@attiksystem.ch>
  0 siblings, 1 reply; 3+ messages in thread

From: Rod Taylor @ 2004-10-20 13:05 UTC (permalink / raw)
  To: Philippe Lang <philippe.lang@attiksystem.ch>; +Cc: pgsql-sql

On Wed, 2004-10-20 at 08:50, Philippe Lang wrote:
> Hello,
> 
> Is it possible to reuse a previously calculated column in a following
> column, like:

SELECT col1
     , col2
     , col2 * 0.75 AS col3
  FROM (SELECT foo.val1 AS col1
             , long_calculation(foo.val1) AS col2
          FROM foo) AS tab;





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Reuse previously calculated column in sql query?
@ 2004-10-21 01:31  Iain <iain@mst.co.jp>
  parent: Rod Taylor <pg@rbt.ca>
  0 siblings, 0 replies; 3+ messages in thread

From: Iain @ 2004-10-21 01:31 UTC (permalink / raw)
  To: Rod Taylor <pg@rbt.ca>; Philippe Lang <philippe.lang@attiksystem.ch>; +Cc: pgsql-sql

Using the the sub-select is one way, but there is another way.

If the function can be declared as strict or immutable the you can call it 
as many times as you like in a single transaction and it will only be 
evaluated once. As far as I know this does work as advertised.

Check the SQL commands reference section of the manual for CREATE FUNCTION

Regards
Iain
----- Original Message ----- 
From: "Rod Taylor" <pg@rbt.ca>
To: "Philippe Lang" <philippe.lang@attiksystem.ch>
Cc: <pgsql-sql@postgresql.org>
Sent: Wednesday, October 20, 2004 10:05 PM
Subject: Re: [SQL] Reuse previously calculated column in sql query?


> On Wed, 2004-10-20 at 08:50, Philippe Lang wrote:
>> Hello,
>>
>> Is it possible to reuse a previously calculated column in a following
>> column, like:
>
> SELECT col1
>     , col2
>     , col2 * 0.75 AS col3
>  FROM (SELECT foo.val1 AS col1
>             , long_calculation(foo.val1) AS col2
>          FROM foo) AS tab;
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
>               http://www.postgresql.org/docs/faqs/FAQ.html 




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2004-10-21 01:31 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-10-20 12:50 Reuse previously calculated column in sql query? Philippe Lang <philippe.lang@attiksystem.ch>
2004-10-20 13:05 ` Rod Taylor <pg@rbt.ca>
2004-10-21 01:31   ` Iain <iain@mst.co.jp>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox