SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


return zero for months where no data exsits


return zero for months where no data exsits

Author
Message
madhavsinghk
madhavsinghk
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 28
Thanks.
Here is the final SQL i have:
I created a ReportDates table with the month, year



SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [ReportDates](
[DateMonth] [int] NULL,
[DateMonthName] [nchar](10) NULL,
[DateYear] [int] NULL,
[ThisDate] [datetime] NULL
) ON [PRIMARY]

GO

==================================================================================================
--Populate ReportDates table

DECLARE @span int
DECLARE @CurrentDate datetime
DECLARE @StartRange datetime
DECLARE @EndRange datetime
DECLARE @tmp datetime

set @span = 0 --
set @StartRange = DATEADD(MM, @span, dateadd(yyyy, -5, GETDATE())) -- 2008-01-01
set @EndRange = DATEADD(MM, @span, dateadd(yyyy, 8, GETDATE())) --- 2020-12-01

SET @CurrentDate = @StartRange

-- insert all dates into temp table
WHILE @CurrentDate <= @EndRange
BEGIN

SET @tmp = DATEADD(MONTH, DATEDIFF(MONTH, 0, @CurrentDate), 0)
IF @CurrentDate < @EndRange
INSERT INTO ReportDates values(Month(@CurrentDate), DATENAME(MM, @CurrentDate), YEAR(@CurrentDate), @tmp)

SET @CurrentDate = dateadd(mm, 1, @CurrentDate)

END

====================================================================================================

Select
COUNT(OBJECTID) AS NewUsers,
s.DateMonthName as MonthName,
s.DateMonth AS MonthNumber,
s.DateYear AS [Year]

FROM
(SELECT DISTINCT
DateMonth, DateYear, DateMonthName
FROM ReportDates
WHERE ThisDate BETWEEN DATEADD(MM, -6, GETDATE()) AND DATEADD(MM, 0, GETDATE())
) s
LEFT JOIN Users ON MONTH(RegisteredDate) = s.DateMonth AND YEAR(RegisteredDate) = s.DateYear
GROUP BY
s.DateMonth,
s.DateYear,
s.DateMonthName
ORDER BY s.DateYear


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search