Here are two options... 1st one is keeping w/ the CTEs and the 2nd replaces them w/ temp tables.
They both produce the desired result but one should run better than the other against your production data.
Test Data...
IF OBJECT_ID('tempdb..#TestData', 'U') IS NOT NULL
DROP TABLE #TestData;
CREATE TABLE #TestData (
CustID INT NOT NULL,
ClientID INT NOT NULL,
StartDate DATE NOT NULL,
EndDate DATE NOT NULL,
PRIMARY KEY CLUSTERED (CustID, ClientID, StartDate)
);
INSERT #TestData (CustID, ClientID, StartDate, EndDate) VALUES
(1000, 5000, '05/10/2017', '05/12/2017'),
(1000, 5000, '05/16/2017', '05/16/2017'),
(1000, 5000, '05/25/2017', '05/25/2017'),
(1100, 5100, '05/01/2017', '05/05/2017'),
(1100, 5100, '05/22/2017', '05/26/2017'),
(1100, 5100, '07/10/2017', '07/12/2017'),
(1200, 5200, '05/01/2017', '07/14/2017');
--==========================================================
#1 CTE...
WITH
cte_AddRN AS (
SELECT
td.CustID, td.ClientID, td.StartDate, td.EndDate,
RN = ROW_NUMBER() OVER (PARTITION BY td.CustID, td.ClientID ORDER BY td.StartDate)
FROM
#TestData td
),
cte_Find6wkGaps AS (
SELECT
ar1.CustID, ar1.ClientID, ar1.StartDate, ar1.EndDate, ar1.RN,
Weeks = ISNULL(NULLIF(CEILING(DATEDIFF(dd, ar1.StartDate, ar1.EndDate) / 7.0), 0), 1),
StartRowGroup = CASE WHEN DATEDIFF(dd, ISNULL(ar2.EndDate, '1900-01-01'), ar1.StartDate) / 7.0 > 6 THEN 1 ELSE 0 END
FROM
cte_AddRN ar1
LEFT JOIN cte_AddRN ar2
ON ar1.CustID = ar2.CustID
AND ar1.ClientID = ar2.ClientID
AND ar1.RN = ar2.RN + 1
),
cte_FillDateGroup AS (
SELECT
fg1.CustID, fg1.ClientID, fg1.StartDate, fg1.EndDate, fg1.RN, fg1.Weeks, fg1.StartRowGroup, x.DateGroup
FROM
cte_Find6wkGaps fg1
OUTER APPLY (
SELECT
DateGroup = MAX(fg2.StartRowGroup * fg2.RN)
FROM
cte_Find6wkGaps fg2
WHERE
fg1.CustID = fg2.CustID
AND fg1.ClientID = fg2.ClientID
AND fg1.EndDate >= fg2.EndDate
) x
)
SELECT TOP 1 WITH TIES
fdg.CustID,
fdg.ClientID,
SUM(fdg.Weeks)
FROM
cte_FillDateGroup fdg
GROUP BY
fdg.CustID,
fdg.ClientID,
fdg.DateGroup
ORDER BY
ROW_NUMBER() OVER (PARTITION BY fdg.CustID, fdg.ClientID ORDER BY fdg.DateGroup DESC);
#2 Temp Tables...
IF OBJECT_ID('tempdb..#AddRN', 'U') IS NOT NULL
DROP TABLE #AddRN;
IF OBJECT_ID('tempdb..#Find6wkGaps', 'U') IS NOT NULL
DROP TABLE #Find6wkGaps;
IF OBJECT_ID('tempdb..#FillDateGroup', 'U') IS NOT NULL
DROP TABLE #FillDateGroup;
SELECT
td.CustID, td.ClientID, td.StartDate, td.EndDate,
RN = ISNULL(ROW_NUMBER() OVER (PARTITION BY td.CustID, td.ClientID ORDER BY td.StartDate), 0)
INTO #AddRN
FROM
#TestData td;
SELECT
ar1.CustID, ar1.ClientID, ar1.StartDate, ar1.EndDate, ar1.RN,
Weeks = ISNULL(NULLIF(CEILING(DATEDIFF(dd, ar1.StartDate, ar1.EndDate) / 7.0), 0), 1),
StartRowGroup = CASE WHEN DATEDIFF(dd, ISNULL(ar2.EndDate, '1900-01-01'), ar1.StartDate) / 7.0 > 6 THEN 1 ELSE 0 END
INTO #Find6wkGaps
FROM
#AddRN ar1
LEFT JOIN #AddRN ar2
ON ar1.CustID = ar2.CustID
AND ar1.ClientID = ar2.ClientID
AND ar1.RN = ar2.RN + 1;
SELECT
CustID = ISNULL(fg1.CustID, 0), ClientID = ISNULL(fg1.ClientID, 0), StartDate = ISNULL(fg1.StartDate, '1900-01-01'), fg1.EndDate, fg1.RN, fg1.Weeks, fg1.StartRowGroup, DateGroup = ISNULL(x.DateGroup, 0)
INTO #FillDateGroup
FROM
#Find6wkGaps fg1
OUTER APPLY (
SELECT
DateGroup = MAX(fg2.StartRowGroup * fg2.RN)
FROM
#Find6wkGaps fg2
WHERE
fg1.CustID = fg2.CustID
AND fg1.ClientID = fg2.ClientID
AND fg1.EndDate >= fg2.EndDate
) x
ALTER TABLE #FillDateGroup ADD PRIMARY KEY CLUSTERED (CustID ASC, ClientID ASC, DateGroup DESC, StartDate ASC);
SELECT TOP 1 WITH TIES
fdg.CustID,
fdg.ClientID,
SUM(fdg.Weeks)
FROM
#FillDateGroup fdg
GROUP BY
fdg.CustID,
fdg.ClientID,
fdg.DateGroup
ORDER BY
ROW_NUMBER() OVER (PARTITION BY fdg.CustID, fdg.ClientID ORDER BY fdg.DateGroup DESC);