I have data in SQL Server like the following
DROP TABLE IF EXISTS #test;
CREATE TABLE #test (id NVARCHAR(20), yr CHAR(4), mo CHAR(2), yr_mo CHAR(7), val int);
INSERT INTO
#test (id, yr, mo, yr_mo, val)
VALUES
('bob', '2023', '01', '2023_01', 100),
('bob', '2023', '02', '2023_02', 75),
('bob', '2023', '03', '2023_03', 0),
('bob', '2023', '04', '2023_04', 20),
('bob', '2023', '05', '2023_05', 60),
('jennifer', '2023', '01', '2023_01', 0),
('jennifer', '2023', '02', '2023_02', 10);
I'd like to PIVOT
the data so that the yr_mo
row values become columns up until a specified point in time (like current month 2023_08
). However I would like to pivot it so that the data fills in 0s up until the declared "stop date" which for the example above would be current date 2023_08
I'm looking for a solution that if I declared that dynamic value, I could create a n columns up until that date with the appropriate data.
Desired outcome sample:
DROP TABLE IF EXISTS #desired;
CREATE TABLE #desired (
id NVARCHAR(20),
[2023_01] int,
[2023_02] int,
[2023_03] int,
[2023_04] int,
[2023_05] int,
[2023_06] int,
[2023_07] int,
[2023_08] int -- current month, would like to dynamically declare this
);
INSERT INTO #desired (
id,
[2023_01],
[2023_02],
[2023_03],
[2023_04],
[2023_05],
[2023_06],
[2023_07],
[2023_08]
)
VALUES
('bob', 1, 100, 75, 0, 20, 60, 0, 0),
('jennifer', 0, 10, 0, 0, 0, 0, 0, 0);
My current attempt:
SELECT
id,
ISNULL(pvt.[2023_01], 0) AS '2023_01',
ISNULL(pvt.[2023_02], 0) AS '2023_02',
ISNULL(pvt.[2023_03], 0) AS '2023_03',
ISNULL(pvt.[2023_04], 0) AS '2023_04',
ISNULL(pvt.[2023_05], 0) AS '2023_05',
ISNULL(pvt.[2023_06], 0) AS '2023_06',
ISNULL(pvt.[2023_07], 0) AS '2023_07',
ISNULL(pvt.[2023_08],0) AS '2023_08'
FROM (
SELECT id, yr_mo, val FROM #test
) AS src
PIVOT (
SUM(val)
FOR yr_mo in ([2023_01], [2023_02], [2023_03], [2023_04], [2023_05], [2023_06], [2023_07], [2023_08])
) AS pvt;
This is not dynamic as I have to list out every single month by hand.