I am using SQL Server 2008
I am trying to tally the wins and losses for any given bike. Each time a user votes, he casts a vote for one bike (1) and a vote against another bike (0).
My vote table looks like this:
VoteID --- BikeID ---- Vote
1 100 1
2 101 0
3 100 0
4 101 1
5 102 1
6 100 0
7 102 0
8 101 1
I want my results to look like this when I run a query for a specific bike
Wins -- Losses
5 6
Right now, my results look like this:
Wins --- Losses
5 NULL
NULL 6
My query looks like this:
SELECT SUM(CASE WHEN Vote = 1 THEN 1 END) AS Wins,
SUM(CASE WHEN Vote = 0 THEN 1 END) AS Losses
FROM Votes
WHERE BikeID = 101
GROUP BY Vote
What do I need to do to get the results on one line?
The problem is your case statements did not cover all conditions, and so were returning null for the cases where they did not account for.
Also, you did not need the group by vote, since you aren’t actually selecting the vote outside the aggregates.