首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SEDE修改查询以处理多个用户

使用SEDE修改查询以处理多个用户
EN

Stack Overflow用户
提问于 2014-04-29 12:34:48
回答 1查看 64关注 0票数 0

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

如果我为一个UserId: 2140173运行它,它似乎运行良好。

代码语言:javascript
复制
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

它回来了

代码语言:javascript
复制
UserId    Total    Accepted    Rejected
2140173    2230        1145        1085

但是,当我试图稍微修改它并为所有有2000多个信誉的用户运行它时,它并没有给出正确的结果:/

我被分选择语句困住了,因为我不知道应该在它们的where子句中放什么。

这是我尝试过的,但是它返回总数,我希望它是每个用户Id的计数。

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-29 13:03:37

试试这个:

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23364825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档