Calculations on the projection (select
) values are useful and common. Yet column aliases declared in the projection can't be used there.
select sellprice as x, cost as y, x/y as markup
from mytable
gives an error.
I can accomplish this with
select sellprice as x, cost as y, sellprice/cost as markup
from mytable
But that is a simple example: We usually declare column aliases names with as
for complex cases. For example, instead of cost
, I might have some function built from average
etc, that I declare as a column alias.
How can I use these aliases inside the projection clause?
Best Answer
The usual way to cope with that is to use a derived table (aka sub-query):