SELECT FORMAT(CreationDate, 'yyyy-MM') AS MonthYear, SUM(...

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

-- Get monthly counts of questions, answers, and total posts
-- from November 2020 to November 2022
SELECT 
    FORMAT(CreationDate, 'yyyy-MM') AS MonthYear,
    SUM(CASE WHEN PostTypeId = 1 THEN 1 ELSE 0 END) AS QuestionCount,
    SUM(CASE WHEN PostTypeId = 2 THEN 1 ELSE 0 END) AS AnswerCount,
    COUNT(*) AS TotalPosts,
    SUM(CASE WHEN AcceptedAnswerId IS NOT NULL THEN 1 ELSE 0 END) as AnsweredQs
FROM 
    Posts
WHERE 
     CreationDate >= '2020-11-01'
    AND CreationDate <= '2022-11-30'
    AND PostTypeId IN (1, 2)  -- 1 for questions, 2 for answers
GROUP BY 
    FORMAT(CreationDate, 'yyyy-MM')
ORDER BY 
    MonthYear ASC;

Enter Parameters

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