加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql server使用计算列

发布时间:2020-12-12 08:25:38 所属栏目:MsSql教程 来源:网络整理
导读:我有这样的查询: select (price1 + price2 + price3) as total_price from prices 我如何使用计算列total_price来计算其他总数? select (price1 + price2 + price3) as total_price,(price4 + total_price) as total_price2from prices 这可能吗? 解决方法
我有这样的查询:
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读