Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7170547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:14:06+00:00 2026-05-28T15:14:06+00:00

I am using SQL Server 2008. I am trying to do some basic math

  • 0

I am using SQL Server 2008.

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.

What I want to do is something like this:

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

Here’s what I am doing now:

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

What is the easiest, cleanest way to do simple math calculations like this in a query?

*EDIT: *

While I got some great answers, I didn’t get what I was looking for.

The scores that I will be calculating are for a specific team, so, my results need to be like this:

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

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:14:07+00:00Added an answer on May 28, 2026 at 3:14 pm

    One way is to wrap your query in an external one:

    SELECT TotalWins,
           TotalLosses,
           TotalWins + TotalLosses as TotalPlays,
           TotalPlays / TotalWins  as PctWins
    FROM
    ( 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
      FROM ...
    )
    

    Another way (suggested by @Mike Christensen) is to use Common Table Expressions (CTE):

    ; WITH Calculation AS 
        ( 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
          FROM ...
        )
    
    SELECT TotalWins,
           TotalLosses,
           TotalWins + TotalLosses as TotalPlays,
           TotalPlays / TotalWins  as PctWins
    FROM
           Calculation 
    

    Sidenote: No idea if this would mean any preformance difference in SQL-Server but you can also write these sums:

    SUM(case when vote = 1 then 1 else 0 end)
    

    as counts:

    COUNT(case when vote = 1 then 1 end)    --- the ELSE NULL is implied
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Server 2008... I'm having some troubles in trying to order my rows
I am trying to import some data from Sql Server 2008 into R, using
I'm using SQL Server 2008 (non-R2) and trying to use Report Builder 3.0. When
I am using SQL Server 2008 developer edition. I was trying to attach the
I am using SQL Server 2008 & 2005 (Express). I'm trying to extract part
I've just installed SQL Server 2008 Developer edition and I'm trying to connect using
I'm trying to export records from SQL Server 2008 to mdb file using OpenDataSource.
I'm trying to setup the Entity Framework with SQL Server 2008. I'm using Guids
Using SQL Server 2008 Reporting services: I'm trying to write a report that displays
I'm having some fun trying to pick a decent SQL Server 2008 spatial index

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.