-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, PrTitle NVARCHAR(100), ChangeDateMsk DATETIME);
INSERT INTO @tbl (PrTitle, ChangeDateMsk) VALUES
(N'#332274 Импорт', '2020-01-25T15:01:24'),
(N'#332274 Импорт', '2020-01-25T15:01:27'),
(N'#332274 Импорт', '2020-05-25T15:01:24'),
(N'#334244 Экспорт', '2020-10-25T05:01:24'),
(N'#334244 Экспорт', '2020-10-25T07:01:24');
-- DDL and sample data population, end
DECLARE @separator CHAR(2) = ', ';
;WITH rs AS
(
SELECT *
, IIF(LEAD(ChangeDateMsk) OVER (PARTITION BY PrTitle ORDER BY ChangeDateMsk) <= DATEADD(MINUTE, 5, ChangeDateMsk), 'true', 'false') AS [outcome]
FROM @tbl
)
SELECT PrTitle
, STUFF((SELECT @separator + FORMAT(ChangeDateMsk, 'yyyy-MM-dd HH:mm:ss') AS [text()]
FROM rs AS O
WHERE O.PrTitle = C.PrTitle
AND o.outcome = 'false'
FOR XML PATH('')), 1, LEN(@separator), NULL) AS ChangeDateMskList
FROM rs AS c
WHERE c.outcome = 'false'
GROUP BY PrTitle;
这是一个概念性的例子。
该解决方案做了两件事:
SQL
结果