3

I am trying to replicate the top user page with the users sorted by number of posts.

Suppose there are two users:

User 'A' with 5608 post (Answers + questions) And User 'B' with 2627 post(Answers + question). The query results should be:

List 
User A
User B

So far I have written a query to list the top 100 users with the most posts (answers and questions). The query is below:

Select Top 100 
       OwnerUserId as [User Link]
       , sum(score) score
       , count(*) [# posts]
       , sum(case posttypeid when 1 then score else 0 end) [question score]
       , sum(case posttypeid when 2 then score else 0 end) [answer score]
       , sum(case posttypeid when 1 then 1 else 0 end) [# questions]
       , sum(case posttypeid when 2 then 1 else 0 end) [# answers]
from Posts p
inner join posttags pt on pt.postid = coalesce(p.parentid, p.id)

WHERE 
 owneruserid is not null
and posttypeid in (1,2) -- only questions and answers
and communityowneddate is null
group by owneruserid    
order by sum(score) desc

This query is sorting results incorrectly. It shows:

2. BalusC (Post 17035 post)

3. Darin Dimitrov (post 21486)

The one with more posts is at the third rank. I could not figure out what is wrong, and could not write a correct query for it.

I have referred to these posts:

1 Answer 1

3

First of all, the join with PostTags causes posts to count multiple times, depending on how many tags they have.

Also, you're ordering by sum(score) desc - that's the total score of the posts. If you want to order by the # of posts, just order by count(*) desc, like this query.

enter image description here

8
  • You query show jon skeet has 99031 post but he has answered 34695 answers and asked 50 questions. @Glorfindel
    – user1
    Commented May 11, 2019 at 18:13
  • That's because you're joining with PostTags, which isn't necessary here. I'll modify the query ...
    – Glorfindel Mod
    Commented May 11, 2019 at 18:14
  • Have you edited your query so that jon skeet's total post should be Answres plus question = 34745 (50q + 34 695 A). Right now query gave Jon skeet's post = 34592 @Golrfindel
    – user1
    Commented May 11, 2019 at 18:25
  • Your query (not mine, I just copied it) excludes Community Wiki posts, because of this clause: and communityowneddate is null.
    – Glorfindel Mod
    Commented May 11, 2019 at 18:27
  • (also, SEDE is updated once a week, on Sunday morning, so this week's posts aren't reflected in the query results)
    – Glorfindel Mod
    Commented May 11, 2019 at 18:30
  • I removed community null part and ran query it gave 34735 but it should be 34745 there is a difference of 10 post why? data.stackexchange.com/stackoverflow/query/1046443 @glorfindel
    – user1
    Commented May 11, 2019 at 18:33
  • See my previous comment ...
    – Glorfindel Mod
    Commented May 11, 2019 at 18:34
  • How can i contact you? @Glorfindel my email is [email protected] please send me your email. I never spam or misuse email
    – user1
    Commented May 11, 2019 at 20:58

You must log in to answer this question.

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