I am having a hard time getting my query to do what I want it to do...
If I run it for one UserId: 2140173 it seems to be working fine
Select UserId,
(Select Count(VoteTypeId) From SuggestedEditVotes where UserId = 2140173) as 'Total',
(Select Count(VoteTypeId) From SuggestedEditVotes where UserId = 2140173 and VoteTypeId = 2) As 'Accepted',
(Select Count(VoteTypeId) From SuggestedEditVotes where UserId = 2140173 and VoteTypeId = 3) As 'Rejected'
from SuggestedEditVotes
inner join Users on SuggestedEditVotes.UserId = Users.Id
where Users.Reputation > 2000 and UserId = 2140173
group by UserId
having Count(VoteTypeId) > 0
it returns
UserId Total Accepted Rejected
2140173 2230 1145 1085
But when I am trying to modify it slightly and run it for all users with more than 2000 reputation it does not give me the correct results :/
I am stuck with the sub Select statements as I am not sure what to put in their where
clause..
This is what I have tried but it returns the totals and I want it to be a count for each user Id
Select UserId,
(Select Count(VoteTypeId) From SuggestedEditVotes) as 'Total',
(Select Count(VoteTypeId) From SuggestedEditVotes where VoteTypeId = 2) As 'Accepted',
(Select Count(VoteTypeId) From SuggestedEditVotes where VoteTypeId = 3) As 'Rejected'
from SuggestedEditVotes
inner join Users on SuggestedEditVotes.UserId = Users.Id
where Users.Reputation > 2000
group by UserId
having Count(VoteTypeId) > 0
Can anyone help?
NOTE: https://data.stackexchange.com/stackoverflow/query/new
Try this:
SELECT UserId,
COUNT(VoteTypeId) AS 'Total',
COUNT(
CASE
WHEN VoteTypeId = 2
THEN VoteTypeId
ELSE NULL
END) AS 'Accepted',
COUNT(
CASE
WHEN VoteTypeId = 3
THEN VoteTypeId
ELSE NULL
END) AS 'Rejected'
FROM SuggestedEditVotes
INNER JOIN Users
ON SuggestedEditVotes.UserId = Users.Id
WHERE Users.Reputation > 2000
GROUP BY UserId
HAVING COUNT(VoteTypeId) > 0