From: | "Oliveiros d'Azevedo Cristina" <oliveiros(dot)cristina(at)asperger-talents(dot)com> |
---|---|
To: | "Rob Richardson" <RDRichardson(at)rad-con(dot)com>, <pgsql-novice(at)postgresql(dot)org> |
Subject: | Re: How can I re-use an expression in a SELECT? |
Date: | 2012-07-06 18:16:56 |
Message-ID: | 13C88A99EA4A4130BEAE0A8E3007C35B@Moon |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-novice |
U're welcome :-)
Best,
Oliveiros
----- Original Message -----
From: Rob Richardson
To: pgsql-novice(at)postgresql(dot)org
Sent: Friday, July 06, 2012 2:45 PM
Subject: Re: [NOVICE] How can I re-use an expression in a SELECT?
I understand. Thanks very much!
RobR
From: Oliveiros d'Azevedo Cristina [mailto:oliveiros(dot)cristina(at)marktest(dot)pt]
Sent: Friday, July 06, 2012 9:30 AM
To: Rob Richardson; pgsql-novice(at)postgresql(dot)org
Subject: Re: [NOVICE] How can I re-use an expression in a SELECT?
I don't know any way of doing it without using a sub-query...
SELECT heat_time, cool_time, fire_date + interval '1 minute' * heat time as end_of_fire
FROM (
select charge,
case when c.rev_heat_time = 0 then c.pred_heat_time
else c.rev_heat_time
end as heat_time,
case when c.rev_cool_time = 0 then c.pred_cool_time
else c.rev_cool_time
end as cool_time
from charge c
) subquery
Surely there are more elegant ways of doing this in later versions.
I'm still using 8.3 ...
Best,
Oliveiros
----- Original Message -----
From: Rob Richardson
To: pgsql-novice(at)postgresql(dot)org
Sent: Friday, July 06, 2012 2:04 PM
Subject: [NOVICE] How can I re-use an expression in a SELECT?
Greetings!
It seems to me, from my uneducated perspective, that if I have a calculated result with a name in a SELECT query, then I ought to be able to re-use the name later in the statement:
select charge,
case when c.rev_heat_time = 0 then c.pred_heat_time
else c.rev_heat_time
end as heat_time,
case when c.rev_cool_time = 0 then c.pred_cool_time
else c.rev_cool_time
end as cool_time,
fire_date + interval '1 minute' * heat_time as end_of_fire
from charge c
Of course, that fails because heat_time cannot be used to calculate end_of_fire. I would have to repeat the case statement that defines it instead. How can I avoid repeating complicated pieces of SELECT queries like this?
Thanks very much!
RobR
From | Date | Subject | |
---|---|---|---|
Next Message | Ioannis Anagnostopoulos | 2012-07-07 01:41:24 | Recheck condition... |
Previous Message | Rob Richardson | 2012-07-06 13:45:02 | Re: How can I re-use an expression in a SELECT? |