@ScottPletcher - helped me out on my previous thread, but now (for me at least) there is an extra layer of complexity. I now need to add in a where clause to include an employee name and have the data for Jan - Dec for 2018 and 2019 show for this employee.
So the data should read like
Jan 2018
Jan 2019
Feb 2018
Feb 2019
Altering the query to add in the WHERE clause has broken it, lol. Can someone assist with this issue?
CREATE TABLE PrevYear (
[EmployeeNumber] char(8) NOT NULL,
[SaleAmount] int DEFAULT NULL,
[SaleDate] date NOT NULL,
[EmployeeName] char(17) NOT NULL
);
CREATE TABLE CurrentYear (
[EmployeeNumber] char(8) NOT NULL,
[SaleAmount] int DEFAULT NULL,
[SaleDate] date NOT NULL,
[EmployeeName] char(17) NOT NULL
);
INSERT INTO CurrentYear
VALUES ('ea12', '100', '2019-01-10', 'Sam Smith');
INSERT INTO CurrentYear
VALUES ('ea12', '199', '2019-01-13', 'Sam Smith');
INSERT INTO CurrentYear
VALUES ('ea12', '100', '2019-03-01', 'Sam Smith');
INSERT INTO CurrentYear
VALUES ('ls22', '100', '2019-05-01', 'Sam Smith');
INSERT INTO PrevYear
VALUES ('ea12', '100', '2018-01-10', 'Sam Smith');
INSERT INTO PrevYear
VALUES ('ea12', '199', '2018-01-13', 'Sam Smith');
INSERT INTO PrevYear
VALUES ('ea12', '100', '2018-03-01', 'Sam Smith');
INSERT INTO PrevYear
VALUES ('ls22', '100', '2018-05-01', 'Sam Smith');
DECLARE @start_date date
DECLARE @number_of_months int
SET @start_date = '20190101'
SET @number_of_months = 12
;WITH
cte_input_values AS (
/* insure @start_date is the first day of the month */
SELECT CAST(DATEADD(MONTH, DATEDIFF(MONTH, 0, @start_date), 0) AS date) AS start_date
),
cte_tally10 AS (
SELECT * FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) AS numbers(number)
),
cte_tally100 AS (
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) - 1 AS month_number
FROM cte_tally10 c1
CROSS JOIN cte_tally10 c2
),
cte_months_to_pull AS (
SELECT DATEADD(MONTH, t.month_number, start_date) AS month_to_pull
FROM cte_tally100 t
CROSS JOIN cte_input_values
WHERE t.month_number BETWEEN 0 AND @number_of_months - 1
UNION ALL
SELECT DATEADD(MONTH, t.month_number, DATEADD(YEAR, -1, start_date)) AS month_to_pull
FROM cte_tally100 t
CROSS JOIN cte_input_values
WHERE t.month_number BETWEEN 0 AND @number_of_months - 1
)
SELECT
STUFF(CONVERT(varchar(12), month_to_pull, 7), 4, 4, '') AS month_year,
ISNULL(YRS.SaleAmount, 0) AS total_sales
FROM cte_months_to_pull
LEFT OUTER JOIN (
SELECT EmployeeName, DATEADD(MONTH, DATEDIFF(MONTH, 0, SaleDate), 0) AS SaleMonth,
SUM(SaleAmount) AS SaleAmount
FROM CurrentYear
CROSS JOIN cte_input_values
WHERE SaleDate >= start_date AND
SaleDate < DATEADD(MONTH, @number_of_months, start_date)
GROUP BY EmployeeName, DATEADD(MONTH, DATEDIFF(MONTH, 0, SaleDate), 0)
UNION ALL
SELECT EmployeeName, DATEADD(MONTH, DATEDIFF(MONTH, 0, SaleDate), 0) AS SaleMonth,
SUM(SaleAmount) AS SaleAmount
FROM PrevYear
CROSS JOIN cte_input_values
WHERE SaleDate >= DATEADD(YEAR, -1, start_date) AND
SaleDate < DATEADD(MONTH, @number_of_months, DATEADD(YEAR, -1, start_date))
GROUP BY EmployeeName, DATEADD(MONTH, DATEDIFF(MONTH, 0, SaleDate), 0)
) AS YRS ON SaleMonth = month_to_pull
WHERE employeename = 'Sam Smith'
ORDER BY MONTH(month_to_pull), YEAR(month_to_pull)