1

我想找出哪些用户在帖子数量最少(少于 10 个)的情况下声誉最高。但是为什么我不能where在这个加入之前有一个子句?:

SELECT TOP 100 Users.Id, Users.DisplayName AS [Username], Users.Reputation, COUNT(Posts.Id) AS [Post Count] FROM Users
//WHERE COUNT(Posts.Id) < 10
JOIN Posts ON Posts.OwnerUserId = Users.Id 
GROUP BY Users.Id, Users.DisplayName, Users.Reputation
ORDER BY Users.Reputation DESC;

原始用户帖子计数示例查询位于data.stackexchange.com/stackoverflow/query/503051

4

1 回答 1

1

这就是HAVING条款MS 参考)的用途。

你会使用:

SELECT TOP 100 Users.Id, Users.DisplayName AS [Username], Users.Reputation, COUNT(Posts.Id) AS [Post Count] FROM Users
JOIN Posts ON Posts.OwnerUserId = Users.Id
GROUP BY Users.Id, Users.DisplayName, Users.Reputation
HAVING COUNT(Posts.Id) < 10
ORDER BY Users.Reputation DESC;

但在这里,利用了一些SEDE 功能

-- maxRows: How many rows to return:
-- maxPosts: Maximum number of posts a user can have:

SELECT TOP ##maxRows:INT?100##
            'site://u/' + CAST(u.Id AS NVARCHAR) + '|' + u.DisplayName  AS [User]
            , u.Reputation
            , COUNT (p.Id)  AS [Post Count]
FROM        Users u
LEFT JOIN   Posts p         ON (p.OwnerUserId = u.Id  AND  p.PostTypeId IN (1, 2) )  -- Q & A only
GROUP BY    u.Id
            , u.DisplayName
            , u.Reputation
HAVING      COUNT (p.Id) <= ##maxPosts:INT?10##
ORDER BY    u.Reputation DESC
            , [Post Count]
            , u.DisplayName

您可以在 SEDE 中看到它

我特别喜欢没有帖子的具有较高代表的用户

于 2018-10-31T19:02:47.580 回答