5

I am trying to get the score of different users into different tags. I am using the platform https://data.stackexchange.com/ to get that information. For example, writing the query:

Select Id, Reputation from Users where Id in (4333347,213615)

gives me the following result:

Id       Reputation
213615   3344
4333347  3587

However, I want the reputation into different tags like: 213615 => Java : 1500, Python 200. Is there any way I can achieve that?

It is okay if the total score is more than the total reputation. The query aims at seeing the top technologies/expertise of an user.

2
  • 3
    As in - for each user you want to know what contribution each tag had toward their reputation? What if there are multiple tags which go together often (e.g. C# and .NET) - the total could be more than the reputation of the user.
    – Wai Ha Lee
    Commented Oct 16, 2019 at 14:18
  • It's fine if the total goes more than the reputation. I am interested to see the top technologies by a user through this query Commented Oct 16, 2019 at 14:24

1 Answer 1

6

The following pivot query does that for you.

Notice that I didn't do a full/correct reputation calc, just the bits based on voting and neglecting any reputation cap, offered bounties, accepted question. I leave that as an exercise for the reader.

For performance reasons the userid's are scattered around on 3 places. Sorry for that but I didn't fancy making that dynamic.

;with post_rep as
(
  select owneruserid
       , tagname
       , sum(
         case posttypeid 
         when 1 then --Q
           case votetypeid
           when 2 then 5
           when 3 then -2
           else 0
           end
         when 2 then --A
           case votetypeid
           when 1 then 15
           when 2 then 10
           when 3 then -2
           when 9 then bountyamount
           else 0
           end
         else
          0
         end
         ) reputation
  from posts p
  inner join posttags pt on pt.postid = coalesce(p.parentid, p.id)
  inner join tags t on t.id = pt.tagid
  inner join votes v on v.postid = p.id
  where v.votetypeid in (1,2,3,9)
  and owneruserid in (4333347 ,213615)
  group by owneruserid
       , tagname
)
-- Select Id, Reputation from Users where Id in (4333347,213615)

select tagname, [4333347] ,[213615]
from (
   select *
   from post_rep
) as pvt
PIVOT
(
sum(reputation) for owneruserid in ([4333347] ,[213615])
) data
order by [4333347] + [213615] desc

When run today this is what the result looks like:

tags for two users and their rep

Keep in mind SEDE is updated once a week on Sunday. Give a big shout-out to Monica Cellio for the awesome SEDE Tutorial she wrote. Say "Hi" in SEDE chat.

1
  • 2
    I didn’t realize Monica was a SQL expert. She continues to surprise me with her talents.
    – Cody Gray Mod
    Commented Oct 16, 2019 at 20:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .