SELECT DATEPART(YEAR, CreationDate) AS Year, DATEPART(MO...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

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;
*/

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results