Reuse previously calculated column in sql query?
Reuse previously calculated column in sql query?
am 20.10.2004 14:50:11 von philippe.lang
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
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
Re: Reuse previously calculated column in sql query?
am 20.10.2004 15:05:38 von pg
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
Re: Reuse previously calculated column in sql query?
am 21.10.2004 03:31:00 von Iain
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"
To: "Philippe Lang"
Cc:
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
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?
http://archives.postgresql.org
Offtopic: psql
am 21.10.2004 13:58:35 von Sad
Hi
Postgres has a perfect tool - psql
what libraries did you (developers) use to develop psql console and=20
particulary command-line editor.
thnx
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?
http://archives.postgresql.org
Re: Offtopic: psql
am 21.10.2004 14:35:11 von achill
O sad Ýãñáøå óôéò Oct 21, 2004 :
> Hi
>
> Postgres has a perfect tool - psql
> what libraries did you (developers) use to develop psql console and
> particulary command-line editor.
% ldd `which psql`
maybe?
>
> thnx
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org
>
--
-Achilleus
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org