Другой возможный подход - определить группы и получить счет:
Ввод:
CREATE TABLE #Data (
ClientId int,
ProcDate date,
ProcDesc varchar(10)
)
INSERT INTO #Data
(ClientId, ProcDate, ProcDesc)
VALUES
(7180, '20180613', 'Assessment'),
(7180, '20180613', 'POC 20'),
(7180, '20180613', 'POC 4b'),
(7180, '20180620', 'POC 20'),
(7180, '20180620', 'POC 4b'),
(7180, '20180627', 'POC 20'),
(7180, '20180627', 'POC 4b'),
(7180, '20180704', 'Assessment'),
(7180, '20180704', 'POC 20'),
(7180, '20180704', 'POC 4b'),
(7180, '20180711', 'POC 20'),
(7180, '20180718', 'POC 20'),
(7180, '20180718', 'POC 4b'),
(7180, '20180905', 'Assessment'),
(7180, '20180905', 'POC 20'),
(7180, '20180912', 'POC 20'),
(7180, '20180912', 'POC 4b'),
(7180, '20180919', 'POC 20'),
(7180, '20180919', 'POC 4b'),
(2584, '20181003', 'Assessment'),
(2584, '20181003', 'POC 20'),
(2584, '20181003', 'POC 4b'),
(2584, '20181104', 'Assessment'),
(2584, '20181104', 'POC 20'),
(2584, '20181104', 'POC 4b'),
(2584, '20181111', 'POC 20'),
(2584, '20181118', 'POC 20'),
(2584, '20181118', 'POC 4b'),
(7585, '20181104', 'Assessment'),
(7585, '20181104', 'POC 20'),
(7585, '20181104', 'POC 4b'),
(7585, '20181111', 'POC 20'),
(7585, '20181118', 'POC 20'),
(7585, '20181118', 'POC 4b'),
(6581, '20181104', 'CommAssessment'),
(6581, '20181104', 'POC 20'),
(6581, '20181104', 'POC 4b'),
(6581, '20181111', 'POC 20')
Оператор (если поддерживается SUM (...) OVER (ORDER BY ...)
):
;WITH GroupsCTE AS (
SELECT
ClientId, ProcDate, ProcDesc,
SUM(CASE WHEN ProcDesc IN ('Assessment', 'CommAssessment') THEN 1 ELSE 0 END)
OVER (ORDER BY ClientId, ProcDate, CASE WHEN ProcDesc IN ('Assessment', 'CommAssessment') THEN 0 ELSE 1 END) AS Groups
FROM #Data
), CountCTE AS (
SELECT
ClientId, ProcDate, ProcDesc,
COUNT(*) OVER (PARTITION BY Groups) AS [Count]
FROM GroupsCTE
)
SELECT
ClientId, ProcDate, ProcDesc, [Count] - 1
FROM CountCTE
WHERE ProcDesc IN ('Assessment', 'CommAssessment')
ORDER BY ClientId, ProcDate
Оператор (если SUM (...) OVER (ORDER BY ...)
не поддерживается):
;WITH GroupsCTE AS (
SELECT
d.ClientId, d.ProcDate, d.ProcDesc,
c.[Group]
FROM #Data d
CROSS APPLY(
SELECT
SUM(CASE WHEN ProcDesc IN ('Assessment', 'CommAssessment') THEN 1 ELSE 0 END) AS [Group]
FROM #Data
WHERE (ClientId = d.ClientId) AND (ProcDate <= d.ProcDate)
) c
), CountCTE AS (
SELECT ClientId, [Group], COUNT(*) - 1 AS [Count]
FROM GroupsCTE
GROUP BY ClientId, [Group]
)
SELECT
g.ClientId, g.ProcDate, g.ProcDesc,
c.[Count]
FROM GroupsCTE g
CROSS APPLY (
SELECT ClientId, [Group], COUNT(*) - 1 AS [Count]
FROM GroupsCTE
WHERE (ClientId = g.ClientId) AND ([Group] = g.[Group])
GROUP BY ClientId, [Group]
) c
WHERE g.ProcDesc IN ('Assessment', 'CommAssessment')
ORDER BY g.ClientId, g.ProcDate, CASE WHEN g.ProcDesc IN ('Assessment', 'CommAssessment') THEN 0 ELSE 1 END, g.ProcDesc
Вывод:
ClientId ProcDate ProcDesc (No column name)
2584 2018-10-03 Assessment 2
2584 2018-11-04 Assessment 5
6581 2018-11-04 CommAssessment 3
7180 2018-06-13 Assessment 6
7180 2018-07-04 Assessment 5
7180 2018-09-05 Assessment 5
7585 2018-11-04 Assessment 5