title: How to do addition and division of aliased columns in a query?
body: <p>I am using SQL Server 2008.</p>
  
  <p>I am trying to do some basic math in some basic queries. I need to add up wins, losses, total, and percentages.  I usually ask for the raw numbers and then do the calculations once I return my query to page.  I would like to give SQL Server the opportunity to work a little harder.</p>
  
  <p>What I want to do is something like this:</p>
  
  <pre><code>SELECT   SUM(case when vote = 1 then 1 else 0 end) as TotalWins,
           SUM(case when vote = 0 then 1 else 0 end) as TotalLosses,
           TotalWins + TotalLosses as TotalPlays,
           TotalPlays / TotalWins  as PctWins
  </code></pre>
  
  <p>Here's what I am doing now:</p>
  
  <pre><code>SELECT   SUM(case when vote = 1 then 1 else 0 end) as TotalWins,
           SUM(case when vote = 0 then 1 else 0 end) as TotalLosses,
           SUM(case when vote = 1 then 1 else 0 end) + SUM(case when vote = 0 then 1 else 0 end) as Votes
  </code></pre>
  
  <p>What is the easiest, cleanest way to do simple math calculations like this in a query?</p>
  
  <p>*<em>EDIT: *</em></p>
  
  <p>While I got some great answers, I didn't get what I was looking for. </p>
  
  <p>The scores that I will be calculating are for a specific team,  so, my results need to be like this:</p>
  
  <pre><code>TeamID   Team    Wins   Losses  Totals
  1        A's     5      3       8
  2        Bee's   7      9       16
  3        Seas    1      3       4
  
  SELECT   T.TeamID,
           T.Team,
           V.TotalWins,
           V.TotalLosses,
           V.PctWins
  FROM     Teams T 
  
        JOIN 
  
       SELECT  V.TeamID,
               SUM(case when vote = 1 then 1 else 0 end) as V.TotWin,
               SUM(case when vote = 0 then 1 else 0 end) as V.TotLoss
       FROM    Votes V
  GROUP BY V.TeamID
  </code></pre>
  
  <p>I tried a bunch of things, but don't quite know what wrong.  I am sure the JOIN part is where the problem is though.  How do I bring these two resultsets together?</p>