Q&A for professional and enthusiast programmers
-- This query calculates the monthly total number of views on the website over the last 36 months. --WITH MonthlyViews AS ( SELECT DATEPART(YEAR, CreationDate) AS Year, DATEPART(MONTH, CreationDate) AS Month, CreationDate AS CreationDate, CAST(ViewCount as BIGINT) AS ViewCount FROM Posts p WHERE CreationDate >= DATEADD(MONTH, -36, GETDATE()) /* ) SELECT Year, Month, SUM(ViewCount) AS TotalViews FROM MonthlyViews GROUP BY Year, Month ORDER BY Year, Month; */