gpt4 book ai didi

sql - 如何在 where 子句中使用别名?

转载 作者:行者123 更新时间:2023-11-29 16:21:28 25 4
gpt4 key购买 nike

Possible Duplicate:
Referring to a Column Alias in a WHERE Clause

   SELECT
Trade.TradeId,
Isnull(Securities.SecurityType,'Other') SecurityType,
TableName,
CASE
WHEN
SecurityTrade.SecurityId IS NOT NULL
THEN
SecurityTrade.SecurityId
ELSE
Trade.SecurityId
END AS PricingSecurityID,
sum(Trade.Quantity)OVER(Partition by Securities.SecurityType, SecurityTrade.SecurityId,Trade.Price, Buy,Long ) as sumQuantity,
--added porfolio id for Getsumofqantity
Trade.PortfolioId,

Trade.Price,
case
when (Buy = 1 and Long = 1) then 1
when (Buy = 0 and Long = 0) then 1
else 0
end Position
from
Fireball_Reporting..Trade

where porfolioid =5 and Position =1

我想在我的 where 子句中使用 Position =1,它是 case 的别名

case
when (Buy = 1 and Long = 1) then 1
when (Buy = 0 and Long = 0) then 1
else 0
end Position

如何在 where 子句中使用它?

我尝试在 where 子句中直接使用该 CASE 语句,但失败了。

WHERE Trade.SecurityId = @SecurityId AND PortfolioId = @GHPortfolioID AND
(case when (Buy = 1 and Long = 1) then 1 when (Buy = 0 and Long = 0) then 1 else 0 end Position = 1)

最佳答案

SQL-Server docs说:

column_alias can be used in an ORDER BY clause, but it cannot be used in a WHERE, GROUP BY, or HAVING clause.

类似于MySQL doc它说:

Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.

MySQL中,您至少可以reuse aliases in the SELECT clause

关于sql - 如何在 where 子句中使用别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54480230/

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