Plot the site activity by voting, questions, answers and comments, aggregated per week.
Q&A for people looking to bypass life's everyday problems with simple tricks
with VoteNames as ( select pt.Id as PostTypeId, vt.Id as VoteTypeId, case when vt.Id = 1 and pt.Id = 2 then 'Accept' when pt.Id = 1 then 'QuestionVote' else 'AnswerVote' end as VName from VoteTypes vt, PostTypes pt where vt.Id in (1, 2, 3) and pt.Id in (1, 2) ) , Activity as ( select DATEADD(DAY, 7 - DATEPART(WEEKDAY, CONVERT(DATE,u.CreationDate)), CONVERT(DATE,u.CreationDate)) as YearMon, 'NewUser' as Series, count(*) as Action from Users u group by DATEADD(DAY, 7 - DATEPART(WEEKDAY, CONVERT(DATE,u.CreationDate)), CONVERT(DATE,u.CreationDate)) UNION select DATEADD(DAY, 7 - DATEPART(WEEKDAY, v.CreationDate), v.CreationDate) as YearMon, VName as Series, count(*) as Action from Posts p, Votes v, VoteNames as vn where p.Id = v.PostId and v.VoteTypeId in (1, 2, 3) and p.PostTypeId in (1, 2) and v.VoteTypeId = vn.VoteTypeId and p.PostTypeId = vn.PostTypeId group by DATEADD(DAY, 7 - DATEPART(WEEKDAY, v.CreationDate), v.CreationDate), VName UNION select DATEADD(DAY, 7 - DATEPART(WEEKDAY, CONVERT(DATE,p.CreationDate)), Convert(DATE, p.CreationDate)) as YearMon, pt.Name as Series, count(*) as Action from Posts p, PostTypes pt where p.PostTypeId in (1, 2) and p.PostTypeId = pt.Id Group by DATEADD(DAY, 7 - DATEPART(WEEKDAY, CONVERT(DATE,p.CreationDate)), Convert(DATE, p.CreationDate)), pt.Name UNION SELECT DATEADD(DAY, 7 - DATEPART(WEEKDAY, CONVERT(DATE,c.CreationDate)), Convert(DATE, c.CreationDate)) as YearMon, 'Comment' as Series, count(*) as Action from Comments c Group by DATEADD(DAY, 7 - DATEPART(WEEKDAY, CONVERT(DATE,c.CreationDate)), Convert(DATE, c.CreationDate)) ) select * from Activity pivot ( sum(Action) for Series in ([Question],[AnswerAccept],[Answer],[Comment],[QuestionVote],[AnswerVote],[NewUser])) as Pivoted where YearMon >= DATEADD(WEEK, -60, CURRENT_TIMESTAMP) -- previous 60 weeks. and YearMon < CURRENT_TIMESTAMP -- ignore most recent week. order by YearMon