I have a query like this:
select
(price1 + price2 + price3) as total_price
from prices
How can i use the computed column total_price to compute other total like this?
select
(price1 + price2 + price3) as total_price,
(price4 + total_price) as total_price2
from prices
Is this possible?
I'd also consider a computed column on the table if this will used often
Then your query is
This way, you can apply the DRY principle...
No it isn't possible to reference the column alias defined at the same level. Expressions that appear in the same logical query processing phase are evaluated as if at the same point in time.
As Joe Celko says
You can define it in a CTE and then re-use it outside the CTE however.
Example