【问题标题】:Using Stack Exchange Data Explorer (SEDE) to find users by post count and reputation使用 Stack Exchange Data Explorer (SEDE) 按帖子计数和信誉查找用户
【发布时间】:2019-04-04 14:09:49
【问题描述】:

我想找出哪些用户在帖子数量最少(少于 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

【问题讨论】:

    标签: sql sql-server-2017 dataexplorer


    【解决方案1】:

    这就是the HAVING clause (MS reference) 的用途。

    你会使用:

    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 features

    -- 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
    

    你可以see it live in SEDE

    我特别喜欢the users with higher rep that have no posts

    【讨论】:

      猜你喜欢
      • 2019-01-23
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 2017-06-16
      • 2020-03-26
      • 1970-01-01
      相关资源
      最近更新 更多