看看下面的示例脚本。
DECLARE @Table TABLE(
ID VARCHAR(20),
[Date] DATETIME,
Value VARCHAR(10)
)
INSERT INTO @Table SELECT '001','23/Mar/2012','P'
INSERT INTO @Table SELECT '001','24/Mar/2012','A'
INSERT INTO @Table SELECT '001','25/Mar/2012','P'
INSERT INTO @Table SELECT '001','26/Mar/2012','P'
SELECT *
FROM @Table
UPDATE @Table
SET Value = 'P'
FROM @Table t
WHERE t.Value = 'A'
AND
(
SELECT TOP 1
Value
FROM @Table tBelow
WHERE t.ID = tBelow.ID
AND t.Date > tBelow.Date
ORDER BY tBelow.Date DESC
) = 'P' --Previous
AND (
SELECT TOP 1
Value
FROM @Table tBelow
WHERE t.ID = tBelow.ID
AND t.Date < tBelow.Date
ORDER BY tBelow.Date
) = 'P' --Next
SELECT *
FROM @Table