title: How do I use alias in where clause?
body: <blockquote>
  <p><strong>Possible Duplicate:</strong><br />
  <a href="https://stackoverflow.com/questions/8370114/referring-to-a-column-alias-in-a-where-clause">Referring to a Column Alias in a WHERE Clause</a></p>
  </blockquote>
  
  <pre><code>   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   
  </code></pre>
  <p>i want to use  Position =1   in my where clause which is an alias of case</p>
  <pre><code>case
  when (Buy = 1 and Long = 1) then 1
  when (Buy = 0 and Long = 0) then 1
  else 0
  end Position
  </code></pre>
  <p>How can I use it in where clause?</p>
  <p>I tried zo directly use that CASE statement in where clause, but failed.</p>
  <pre><code>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)
  </code></pre>