Subject | RE: [firebird-support] Use of column aliases in a SELECT |
---|---|
Author | Svein Erling Tysvær |
Post date | 2012-01-27T12:04:54Z |
>>> Hi!I don't think there is any way to help you in Firebird 1.5 (except views/stored procedures). In Firebird 2.5, I'd typically recommend using a CTE:
>>>
>>> I find myself repeatedly writing code such as
>>>
>>> SELECT (a+b)*c AS value1,
>>> some_function((a+b)*c) AS value2,
>>> some_other_function((a+b)*c) AS value 3
>>> FROM ...
>>
>>> Is there any other way to "reuse" a calculated value by referencing its
>>> alias than through views? E.g.
>>
>>> CREATE VIEW valueview AS SELECT SELECT (a+b)*c AS value1 FROM ...
>>
>>> SELECT value1, some_function(value1) AS value2 ...
>>
>>> I know I can reference the column by its position in GROUP BY, but I
>>> need to reference it in the field list as well as in the WHERE condition.
>>
>> How about derived tables? - (supported from version 2.0 onwards)
>>
>> http://www.firebirdsql.org/refdocs/langrefupd21-select.html#langrefupd21-derived-tables
>
>I guess. I am just not very familiar with them yet, as I still need to
>support Firebird 1.5. But it's definitely something to keep in mind once
>I get rid of those.
WITH MYCTE(Value1) AS
(SELECT (a+b)*c FROM MyTable)
SELECT Value1,
some_function(Value1) AS value2,
some_other_function(Value1) AS value 3
FROM MYCTE
rather than a derived table:
SELECT Value1,
some_function(Value1) AS value2,
some_other_function(Value1) AS value 3
FROM (SELECT (a+b)*c as Value1 FROM MyTable)
but I guess my main reason (for simple cases like this, I don't think derived tables is an option in more complex cases like recursive CTEs) is that I prefer to read a statement from the top and down rather than "multiparenthesised".
Set