SQL - DataExplorer查询顶级无名用户

时间:2014-08-15 00:32:38

标签: sql stackexchange dataexplorer

As previously discussed on meta

我想创建一个数据资源管理器查询,以显示StackOverflow上前100名最无名的用户。

前100名的意思是按降序排列的零接受答案的最大百分比排序的列表。

这是我第一次尝试使用SQL,我正在研究其他查询,并认为这就是它:

SELECT TOP 100
    u.Id as [User Link],
    count(a.Id) as [Answers],
(select sum(CASE WHEN a.Score = 0 then 1 else 0 end) * 1000 / count(a.Id) / 10.0) as [Percentage]
from
    Users u
    inner join
    Posts q on q.AcceptedAnswerId = u.Id
    inner join
    Posts a
    on a.Id = q.AcceptedAnswerId
where
      a.CommunityOwnedDate is null
      and a.postTypeId = 2
      and u.Reputation > 1000
group by u.Id
order by Percentage DESC

结果:http://data.stackexchange.com/stackoverflow/query/218910

结果显示用户有一个答案,当您检查他们的个人资料时,这不是真的。

1 个答案:

答案 0 :(得分:2)

您可以使用Sam Saffron的The true unsung heros查询来提取此信息。我稍微修改了它,只包括前100名。

select top 100 
  X.*, u.Reputation from (
  select a.OwnerUserId [User Link], 
  sum(case when a.Score = 0 then 0 else 1 end) as [Non Zero Score Answers],  
  sum(case when a.Score = 0 then 1 else 0 end) as [Zero Score Answers]
from Posts q
join Posts a on a.Id = q.AcceptedAnswerId 
where a.CommunityOwnedDate is null and a.OwnerUserId is not null
 and a.OwnerUserId <> isnull(q.OwnerUserId,-1)
group by a.OwnerUserId
having sum(case when a.Score = 0 then 1 else 0 end) > 10
) as X 
join Users u on u.Id = [User Link]
order by --[Zero Score Answers] desc, 
([Zero Score Answers]+ 0.0) / ([Zero Score Answers]+ [Non Zero Score Answers]+ 0.0) desc

按零分答案与总答案的比率进行排序。