It\'s getting difficult to group and display records every 5 days.
Here is my data:
FLIGHT
Here it is my proposed solution:
DECLARE @MinDate AS DATETIME = (SELECT MIN(flight_date) FROM flights);
WITH cte
AS
(
SELECT
flight_date, DATEDIFF(DAY, @MinDate, flight_date) AS NoDays,
DATEDIFF(DAY, @MinDate, flight_date)/5 AS NoGroup,
DPT
FROM flights
)
SELECT
DATEADD(DAY, NoGroup*5, @MinDate) AS [Week Start],
DATEADD(DAY, NoGroup*5+4, @MinDate) AS [Weed End],
SUM(DPT)
FROM cte
GROUP BY NoGroup;
The idea is to form groups of 5 days, then associate a record to a specific group based on division with 5. NoDays represents the days spent from MinDate to Flight_Date.