2

I am trying to gather information on the number of answers per question by week for a user with fewer than 25 reputation points from the math.stackexchange site (and the comment count per question).

I have the following query but it has not worked

-- CTE with year, week and start of week dates
with yw as (
select distinct 
   year(creationdate) as y 
 , datepart(week, creationdate) as w
 -- http://stackoverflow.com/a/3936891/578411
 , cast(
     DATEADD(
          day, 
          1 - DATEPART(dw, creationdate), 
          creationdate) 
      as date
    ) as sdow
from posts
)

-- grouping over the union
select sdow
 , avg(answercount) as [answercount]
from 
(
--answercount from users with reputation < 25
select sdow
   , count(*) as [answercount]
from posts p
inner join users u on u.id = p.owneruserid
right outer join yw 
 on yw.y = year(p.creationdate) 
and yw.w = datepart(week, p.creationdate) 
where u.reputation < 25 -- rep
group by sdow
union
) as total

1 Answer 1

3

Something like this. I've corrected the syntax errors, you have a union of something against nothing for instance.

I've also restricted the posts to questions since that seems to make more sense for your query (p.posttypeid = 1)

I've not done the comment count, maybe you can do that yourself or ask another question if not.

-- CTE with year, week and start of week dates
with yw as (
select distinct 
    year(creationdate) as y 
 , datepart(week, creationdate) as w
 -- http://stackoverflow.com/a/3936891/578411
 , cast(
     DATEADD(
          day, 
          1 - DATEPART(dw, creationdate), 
          creationdate) 
      as date
    ) as sdow
from posts
)

--answercount from users with reputation < 25
select sdow
, avg(cast(p.answercount as float)) as [answercount]
from posts p
inner join users u on u.id = p.owneruserid
right outer join yw 
 on yw.y = year(p.creationdate) 
and yw.w = datepart(week, p.creationdate) 
where u.reputation < 25 -- rep
and p.posttypeid = 1 -- questions
group by sdow

You can see it run here

5
  • So, I thought that using avg(answercount) would give the average number of answers per question but it doesn't seem that straightforward. Thanks!
    – Sarah
    May 26, 2016 at 4:46
  • Hi Robert, I ran the code and it gave me the number of answers posted per week. Sorry for not being clearly but I wanted to find on a weekly basis, the average number of answers per question. Since most answers on mathstackexchange range from 0-2, I am expecting anywhere from a tad less than 1 to a bit greater than 1. @Robert Longson
    – Sarah
    May 27, 2016 at 17:34
  • I've updated the answer. May 28, 2016 at 21:13
  • Is there any way that I can see more significant figures? @Robert Longson
    – Sarah
    May 29, 2016 at 2:44
  • ok I've fixed that May 29, 2016 at 6:07

You must log in to answer this question.

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