Well there's many ways to solve your problem, based on the limited information you've provided. One way is to create a temporary numbers table with the Days
(maybe this exists as a concrete table already in your database 🤷♂️) and then cross join it to your CalendarEvents
table like so:
DROP TABLE IF EXISTS #Days;
CREATE TABLE #Days (DayNumber INT PRIMARY KEY CLUSTERED);
INSERT INTO #Days
VALUES (1), (2), (3);
SELECT
CE.Event,
IIF(D.DayNumber = 1, CE.People, CE.People - D.DayNumber) AS People
D.DayNumber AS [Day]
FROM CalendarEvents AS CE
CROSS JOIN #Days AS D
ORDER BY CE.Event, D.DayNumber;
This assumes the function that describes your People
column in your expected results dataset is People - Day
, except on the first day where it's just People
. Hard to say if that's true for your entire dataset, with only the limited example provided. (Feel free to explain what the actual function is otherwise.)
Of course you'll need to load more rows into the #Days
temp table if you have more days you need to show too. Alternative sources for a list of days that you can use too are either a date dimensions table, which is globally helpful for a multitude of other problems, or a CTE based on an existing table using the ROW_NUMBER()
window function like so:
WITH _Days AS
(
SELECT
ROW_NUMBER() OVER
(
ORDER BY
object_id,
column_id
) AS DayNumber
FROM sys.columns
)
SELECT
CE.Event,
IIF(D.DayNumber = 1, CE.People, CE.People - D.DayNumber) AS People
D.DayNumber AS [Day]
FROM CalendarEvents AS CE
CROSS JOIN _Days AS D
WHERE D.DayNumber <= 3
ORDER BY CE.Event, D.DayNumber;