我具有以下表结构:
Id, Message 1, John Doe 2, Jane Smith 3, Error 4, Jane Smith
有没有办法获取错误记录和周围的记录?即找到所有错误以及它们之前和之后的记录。
;WITH numberedlogtable AS ( SELECT Id,Message, ROW_NUMBER() OVER (ORDER BY ID) AS RN FROM logtable ) SELECT Id,Message FROM numberedlogtable WHERE RN IN (SELECT RN+i FROM numberedlogtable CROSS JOIN (SELECT -1 AS i UNION ALL SELECT 0 UNION ALL SELECT 1) n WHERE Message='Error')