0

我很难让我的查询做我想做的事......

如果我为一个 UserId: 2140173 运行它,它似乎工作正常

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

它返回

UserId    Total    Accepted    Rejected
2140173    2230        1145        1085

但是当我试图稍微修改它并为所有拥有超过 2000 名声望的用户运行它时,它并没有给我正确的结果:/

我被 sub Select 语句卡住了,因为我不确定在他们的where子句中放什么..

这是我尝试过的,但它返回总数,我希望它是每个用户 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

任何人都可以帮忙吗?

注意:https ://data.stackexchange.com/stackoverflow/query/new

4

1 回答 1

3

试试这个:

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
于 2014-04-29T13:03:37.133 回答