sql server使用计算列

前端之家收集整理的这篇文章主要介绍了sql server使用计算列前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这样的查询
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.

As Joe Celko says

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
原文链接:https://www.f2er.com/mssql/76161.html

猜你在找的MsSQL相关文章