Q&A about the site for developers and end users of the Sitecore CMS and multichannel marketing software
With a as ( Select p.Id, cast(DATEPART(YYYY, p.CreationDate) as char) TimeYr, p.PostTypeId, p.Score, case when VoteTypeId = 2 then 1.00 when VoteTypeId = 3 then -1.00 else 0.00 end VoteS, round(1.00*DATEDIFF(day, p.CreationDate, v.CreationDate)/30,0) delay from Votes v join Posts p on p.Id = v.PostId where p.PostTypeId in (1,2) and VoteTypeId in (2,3) ), c as ( Select TimeYr, count(distinct Id) nyear from a where PostTypeId = 1 group by TimeYr ), d as ( Select delay, TimeYr, SUM(VoteS) sumscore from a where PostTypeId = 1 and delay <= 36 and Cast(TimeYr as int) in (2010,2011,2012,2015) group by delay,TimeYr ) Select d.delay, d.TimeYr, (d.sumscore/c.nyear) from d join c on d.TimeYr = c.TimeYr order by d.TimeYr, d.delay