-3

您将如何为 data.stackexchange 组合一个 SQL 查询,该查询将显示给定位置标签的最活跃用户(根据给出的答案)?

例如。类似于此处列出的前 30 名https://stackoverflow.com/tags/ruby-on-rails-3/topusers但特定于位置。

所以过去 30 天在柏林等地的顶级 Ruby 回答者

谢谢!

4

1 回答 1

1

因此,在查看了数据库模式之后,这就是我提出的查询。

-- Top 10 Ruby Answerers in the last 30 days in Berlin based on score
select top 10
  u.displayname, 
  number_of_answers = count(*), 
  total_score = sum(p.score)
from
  users u                                 
join 
  posts p on p.owneruserid = u.id         -- joined to get answer posts
join 
  posts pp on p.parentid = pp.id          -- post parent is the question
join 
  posttags pt on pt.postid = pp.id        -- tags for post parent
join 
  tags t on t.id = pt.tagid               -- tags for tag name
where 
  t.tagname like '%ruby%'                 -- tags to filter for
 and                                      -- includes everything ruby inc. rails
  p.creationdate > (getdate()-30)         -- past 30 days
 and 
  u.location like '%Berlin%'              -- locations differ in format
group by 
  u.displayname
order by 
  3 desc;                                 -- order by total score for "best" answerers
                                          -- order by 2 (count) to get most active

我不是数据浏览器架构方面的专家,因此查询可能不太正确,并且有一些警告:日期过滤器适用于问题而不是答案,因此可能有用户有更多答案如果他们回答了较旧的问题,则总体上是过去 30 天,而且,由于许多用户根本没有指定位置,因此该位置是一个非常不可靠的字段。它可能尽可能接近。

数据浏览器并不难使用——用它做一些试验,你就会意识到这些表是如何连接的。这是一个很好的锻炼:)

这是查询

于 2015-08-26T12:28:10.643 回答