2012-10-23 2 views
10

가능한 중복 :
Referring to a Column Alias in a WHERE Clausewhere 절에 별칭을 사용하려면 어떻게해야합니까?

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 절에 위치 = 1을 사용하고자하는 경우

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

의 별칭은 내가 어떻게 할 수 where 절에서 사용 하시겠습니까?

내가 직접 나를

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) 
+0

외부 쿼리에 다른 select 및 check postion = 1을 추가하지 않는 이상 수 없습니다. –

+0

어떤 SQL 언어를 사용하고 있습니까? – StingyJack

+0

T SQL in sql server 2008 – Neo

답변

31

표준 SQL 가 WHERE 절에 열 별칭에 대한 참조를 허용하지 않습니다 도와주세요 where 절에서 그 CASE 문을 사용하지만 실패했습니다. WHERE 절이 평가 될 때 컬럼 값이 아직 결정되지 않았기 때문에이 제한이 부과됩니다.

Taken from MySQL Doc

column_alias은 ORDER BY 절에서 사용할 수 있지만 는 WHERE, GROUP BY, 또는 절 데 사용할 수 없습니다.

Taken from the MSSQL Doc

+3

''sql-server'라는 태그가 붙은 답변에서''MySQL Doc에서 가져온 것 '입니까? – MatBailie

13

당신이하지, 직접적으로 할 수 있습니다.

그러나 하위 쿼리에서 전체 쿼리를 래핑하는 경우 제대로 작동합니다.

SELECT 
    * 
FROM 
(
    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 
) 
    AS data 
WHERE 
    Position = 1 

WHERE 절에 CASE 문을 반복 할 필요가 없습니다 것을 의미한다. (유지 및 건조).

또한 옵티마이 당신 단순히 WHERE 절에 자신을 반복했다 것처럼 작동 할 수있는 구조입니다.

다른 RDBMS에도 매우 유용합니다.

SQL 서버에서


, 당신은 또한 다른 옵션을 가지고 ...

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, 
    position.val AS Position 
from 
    Fireball_Reporting..Trade 
CROSS APPLY 
(
    SELECT 
    case 
     when (Buy = 1 and Long = 1) then 1 
     when (Buy = 0 and Long = 0) then 1 
            else 0 
    end AS val 
) 
    AS position 
where 
    porfolioid = 5 
    AND position.val = 1 
+0

Juergen의 대답과 대답은 WHERE 절이 평가 될 때 열 값이 아직 결정되지 않았을 수 있다는 사실과 관련이 있습니다. 하지만 이후로 하위 쿼리를 사용하고 있기 때문에. – Mukus

5

당신은 직접이 작업을 수행 할 수 있습니다 ...하지만 당신은 추가가 모든 주위에 선택 포장 수

select * from 
    ( 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 
    )x 
    where x.position = 1 
0

아마 뭔가를보고 싶어하지만, 확실히이 그것을 다룰 것입니다 : WHERE 절을 사용

WHERE (Buy = 1 and Long = 1) OR (Buy = 0 and Long = 0)

+1

있습니다. 그러나 "자신을 반복하지 마십시오"(DRY)는 유지 보수 및 디버깅을 지원하는 기본 원칙입니다 *. 따라서 한 번 계산을 여러 번 참조 할 수있는 것이 매우 바람직합니다. – MatBailie