thể trùng lặp:
Referring to a Column Alias in a WHERE ClauseLàm cách nào để sử dụng bí danh trong mệnh đề where?
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
tôi muốn sử dụng vị trí = 1 trong mệnh đề where của tôi đó là một bí danh của trường hợp
case
when (Buy = 1 and Long = 1) then 1
when (Buy = 0 and Long = 0) then 1
else 0
end Position
Làm thế nào tôi có thể sử dụng nó trong mệnh đề where?
Tôi đã cố gắng sử dụng trực tiếp mà tuyên bố CASE trong mệnh đề where nhưng thất bại hãy giúp tôi
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)
Bạn không thể trừ khi bạn thêm một lựa chọn khác và kiểm tra postion = 1 trên truy vấn bên ngoài. –
Bạn đang sử dụng ngôn ngữ SQL nào? – StingyJack
T SQL trong máy chủ sql 2008 – Neo