Month wise total count and return zero if month data is not present?

Forums SQLMonth wise total count and return zero if month data is not present?
Staff asked 4 years ago

I have small doubt in SQL. Please tell me how to display month-wise data to solve this issue in the SQL server.

Thanks

Shaikh Hafeezjaha replied 2 years ago

Answers (1)

Add Answer
Faisal Pathan Marked As Accepted
Staff answered 4 years ago

Try below query

SELECT
    [1] AS January,
    [2] AS February,
    [3] AS March,
    [4] AS April,
    [5] AS May,
    [6] AS June,
    [7] AS July,
    [8] AS August,
    [9] AS September,
    [10] AS October,
    [11] AS November,
    [12] AS December
FROM
(Select CreatedDate,MONTH(CreatedDate) as TMonth from PostMasters where YEAR(CreatedDate) = '2022') source
PIVOT
(
    count(CreatedDate)
    FOR TMonth
    IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12] )
) AS pvtMonth

 

 

Subscribe

Select Categories