我尝试在data.stackexchange.com上编写一个查询,它可以回答我写过帖子且被查看超过 1000 次并且拥有某个类别的徽章的用户的百分比是多少。
问题:如果我写AND ViewCount > 1000
,我会得到相同的结果,因为WHERE ViewCount > 1000
我不明白为什么会发生
带有 WHERE 的查询:
SELECT
TOP 1000
Class, (Count(Badges.UserId)* 100 / (SELECT COUNT(*) From Badges)) AS Percentage
FROM Badges
WHERE Badges.UserId IN (SELECT Posts.OwnerUserId FROM Posts
INNER JOIN
Badges ON Posts.OwnerUserId = Badges.UserId
WHERE ViewCount > 1000)
GROUP BY Class
ORDER BY Class
带有 AND 的查询:
SELECT
TOP 1000
Class, (Count(Badges.UserId)* 100 / (SELECT COUNT(*) From Badges)) AS Percentage
FROM Badges
WHERE Badges.UserId IN (SELECT Posts.OwnerUserId FROM Posts
INNER JOIN
Badges ON Posts.OwnerUserId = Badges.UserId
AND ViewCount > 1000)
GROUP BY Class
ORDER BY Class
输出(由于某种原因在两种情况下都是相同的):
1 级 - 2%
2 级 - 15%
3 级 - 45%