Search code examples
sqlstackexchangedataexplorer

SQL - DataExplorer Query Top Unsung Users


As previously discussed on meta:

I want to create a Data Explorer query to show the top 100 most unsung users on StackOverflow.

What I mean by top 100 is a list ordered by biggest % of zero accepted answers in descending order.

This is my first time trying to work with SQL, I was looking into other queries and thought this would be it:

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

Result: https://data.stackexchange.com/stackoverflow/query/218910

The result show users have one answer, which isn't true when you check their profiles.


Solution

  • You can pull this information using Sam Saffron's The true unsung heros query. I've modified it slightly to include only the top 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
    

    This sorts by the ratio of Zero Score answers to total answers.