How can I re-use an expression in a SELECT?

Поиск
Список
Период
Сортировка
От Rob Richardson
Тема How can I re-use an expression in a SELECT?
Дата
Msg-id 67D108EDFAD3C148A593E6ED7DCB4BBD1FDB3CF6@RADCONWIN2K8PDC.radcon.local
обсуждение исходный текст
Ответы Re: How can I re-use an expression in a SELECT?
Список pgsql-novice

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

В списке pgsql-novice по дате отправления:

Предыдущее
От: Andy Halsall
Дата:
Сообщение: Re: Function Scan costs
Следующее
От: Thomas Kellerer
Дата:
Сообщение: Re: How can I re-use an expression in a SELECT?