By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE SALESHS
( IDAREA INT,
DATEREG date NOT NULL,
IDPROD [NVARCHAR](50) NOT NULL
);
/* Insert rows into table 'SALESHS' */
INSERT INTO SALESHS
( IDAREA, DATEREG, IDPROD)
VALUES
( 1, '03/12/2019', 'xplpc'),
( 1, '03/15/2019', 'ndtlctm'),
( 2, '04/12/2019', 'wntd')
/* Create Calendar Table to capture all the dates for first day of month from start Date to end date */
CREATE TABLE Calendar
(
[CalendarDate] DATE
,[MonthName] AS FORMAT(CONVERT(DATE, DATEADD(m, DATEDIFF(m, 0, CalendarDate), 0)), 'MMM-yyyy')
,[MonthNo] AS FORMAT(CalendarDate,'MM')
,[Year] AS FORMAT(CalendarDate,'yyyy')
,DateKey AS CONCAT(FORMAT(CalendarDate,'yyyy'), FORMAT(CalendarDate,'MM'))
)
DECLARE @Date DATE, @StartDate DATE, @EndDate DATE
SET @Date = '01/01/2012'
SET @StartDate = CONVERT(DATE, DATEADD(m, DATEDIFF(m, 0, @Date), 0)) /* Set Start date to first day of the month for given date */
SET @EndDate = '04/01/2019'
WHILE @StartDate <= @EndDate
BEGIN
INSERT INTO Calendar (CalendarDate)
SELECT @StartDate
SET @StartDate = DATEADD(m, 1, @StartDate)
END
IDAREA | Jan-2012 | Feb-2012 | Mar-2012 | Apr-2012 | May-2012 | Jun-2012 | Jul-2012 | Aug-2012 | Sep-2012 | Oct-2012 | Nov-2012 | Dec-2012 | Jan-2013 | Feb-2013 | Mar-2013 | Apr-2013 | May-2013 | Jun-2013 | Jul-2013 | Aug-2013 | Sep-2013 | Oct-2013 | Nov-2013 | Dec-2013 | Jan-2014 | Feb-2014 | Mar-2014 | Apr-2014 | May-2014 | Jun-2014 | Jul-2014 | Aug-2014 | Sep-2014 | Oct-2014 | Nov-2014 | Dec-2014 | Jan-2015 | Feb-2015 | Mar-2015 | Apr-2015 | May-2015 | Jun-2015 | Jul-2015 | Aug-2015 | Sep-2015 | Oct-2015 | Nov-2015 | Dec-2015 | Jan-2016 | Feb-2016 | Mar-2016 | Apr-2016 | May-2016 | Jun-2016 | Jul-2016 | Aug-2016 | Sep-2016 | Oct-2016 | Nov-2016 | Dec-2016 | Jan-2017 | Feb-2017 | Mar-2017 | Apr-2017 | May-2017 | Jun-2017 | Jul-2017 | Aug-2017 | Sep-2017 | Oct-2017 | Nov-2017 | Dec-2017 | Jan-2018 | Feb-2018 | Mar-2018 | Apr-2018 | May-2018 | Jun-2018 | Jul-2018 | Aug-2018 | Sep-2018 | Oct-2018 | Nov-2018 | Dec-2018 | Jan-2019 | Feb-2019 | Mar-2019 | Apr-2019 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 | 0 |
2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |