乍一看,这似乎是一个元问题,但它实际上是关于SQL的。
我很好奇在Stack Overflow上有多少帖子和哪些帖子同时获得了支持和反对。我试图组合一个Data Explorer查询来找出答案,但无法使其正常工作。
我编写了以下子查询,以查找具有一定支持率的帖子:
SELECT PostId as [Post Link], COUNT(v.PostId) AS 'Upvote count'
FROM Votes v
INNER JOIN Posts p
ON p.Id=v.PostId
WHERE PostTypeId = 1
AND VoteTypeId = 2
GROUP BY v.PostId
HAVING COUNT(v.PostId) > 5我正在使用一个类似的方法来投反对票。
我不知道的是,如何编写一个只返回出现在两个子查询结果中的帖子的查询。我所有的尝试在语法上都是不正确的。有没有一个术语可以用来描述我正在尝试做的事情,我可以跳到Google上?也许是一个特殊的运算符或关键字?
发布于 2011-08-09 04:35:00
如果你的意思是,你想拉出具有特定支持率和特定负票数的答案,请尝试以下方法:
SELECT
PostId as [Post Link],
COUNT(CASE VoteTypeId WHEN 2 THEN v.PostId END) AS 'Upvote count',
COUNT(CASE VoteTypeId WHEN 3 THEN v.PostId END) AS 'Downvote count'
FROM Votes v
INNER JOIN Posts p
ON p.Id=v.PostId
WHERE PostTypeId = 1
AND VoteTypeId IN (2, 3)
GROUP BY v.PostId
HAVING COUNT(CASE VoteTypeId WHEN 2 THEN v.PostId END) > 150
AND COUNT(CASE VoteTypeId WHEN 3 THEN v.PostId END) > 10这将为你选择那些同时有超过150个赞成票和超过10个反对票的答案。
发布于 2011-08-10 04:43:12
作为替代方案,可以在Stack Exchange Data Explorer上找到
set nocount on
declare @VoteStats table (PostId int, up int, down int)
insert @VoteStats
select
PostId,
up = sum(case when VoteTypeId = 2 then 1 else 0 end),
down = sum(case when VoteTypeId = 3 then 1 else 0 end)
from Votes
where VoteTypeId in (2,3)
group by PostId
set nocount off
select top 100 p.id as [Post Link] , up, down from @VoteStats
join Posts p on PostId = p.Id
where down > (up * 0.5) and p.CommunityOwnedDate is null and p.ClosedDate is null
order by up deschttps://stackoverflow.com/questions/6986461
复制相似问题