Q&A for professional and enthusiast programmers
select Round(p1.Total * 100.0 / p2.Total, 2) ClosePercentage, p1.Total NClosedPosts, p2.Total NPosts, Round(p1.TotalNegative * 100.0 / p1.Total, 2) ClosedNegativePercentage, p1.TotalNonNegative, p1.TotalNegative, p1.year, p1.month, p1.Reason ReasonId, Name as Reason from( select count(p.Id) Total, count(case when p.Score >= 0 then 1 else null end) TotalNonNegative, count(case when p.Score < 0 then 1 else null end) TotalNegative, YEAR(p.CreationDate) year, MONTH(p.CreationDate) month, ph.Comment Reason from Posts p join PostHistory ph on p.Id = ph.PostId where p.ClosedDate is not null and ph.PostHistoryTypeId = 10 group by YEAR(p.CreationDate), MONTH(p.CreationDate), ph.Comment ) p1 join ( select count(p.Id) Total, YEAR(p.CreationDate) year, MONTH(p.CreationDate) month from Posts p group by YEAR(p.CreationDate), MONTH(p.CreationDate) ) p2 on p1.year = p2.year and p1.month = p2.month join CloseReasonTypes rt on p1.Reason = rt.Id order by p1.year desc, p1.month desc