我有这样的查询:
select (price1 + price2 + price3) as total_price from prices
我如何使用计算列total_price来计算其他总数?
select (price1 + price2 + price3) as total_price,(price4 + total_price) as total_price2 from prices
这可能吗?
解决方法
不可以引用在同一级别定义的列别名.出现在同一逻辑查询处理阶段的表达式为
evaluated as if at the same point in time.
Things happen “all at once” in sql,not “from left to right” as they
would in a sequential file/procedural language model
您可以在CTE中定义它,然后在CTE外重复使用它.
例
WITH T AS (SELECT ( price1 + price2 + price3 ) AS total_price,price4 FROM prices) SELECT total_price,( price4 + total_price ) AS total_price2 FROM T