gpt4 book ai didi

sql server 使用计算列

转载 作者:行者123 更新时间:2023-12-01 16:18:24 24 4
gpt4 key购买 nike

我有一个这样的查询:

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

关于sql server 使用计算列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8135774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com