declare @T table
(
[Date] varchar(10),
Period int,
[Event] varchar(9)
);
-- shortened to 1 hour periods
insert into @T
select '20110501', 1, 'NotImpact' union all
select '20110501', 2, 'NotImpact' union all
select '20110501', 3, 'NotImpact' union all
select '20110501', 4, 'NotImpact' union all
select '20110501', 5, 'NotImpact' union all
select '20110501', 6, 'NotImpact' union all
select '20110501', 7, 'NotImpact' union all
select '20110501', 8, 'NotImpact' union all
select '20110501', 9, 'NotImpact' union all
select '20110501', 10, 'NotImpact' union all
select '20110501', 11, 'NotImpact' union all
select '20110501', 12, 'NotImpact' union all
select '20110501', 13, 'NotImpact' union all
select '20110501', 14, 'NotImpact' union all
select '20110501', 15, 'IMPACT' union all
select '20110501', 16, 'NotImpact' union all
select '20110501', 17, 'NotImpact' union all
select '20110501', 18, 'NotImpact' union all
select '20110501', 19, 'NotImpact' union all
select '20110501', 20, 'NotImpact' union all
select '20110501', 21, 'NotImpact' union all
select '20110501', 22, 'NotImpact' union all
select '20110501', 23, 'NotImpact' union all
select '20110501', 24, 'NotImpact' union all
select '20110601', 1, 'NotImpact' union all
select '20110601', 2, 'NotImpact' union all
select '20110601', 3, 'NotImpact' union all
select '20110601', 4, 'NotImpact' union all
select '20110601', 5, 'NotImpact' union all
select '20110601', 6, 'NotImpact' union all
select '20110601', 7, 'NotImpact' union all
select '20110601', 8, 'NotImpact' union all
select '20110601', 9, 'NotImpact' union all
select '20110601', 10, 'NotImpact' union all
select '20110601', 11, 'NotImpact' union all
select '20110601', 12, 'NotImpact' union all
select '20110601', 13, 'NotImpact' union all
select '20110601', 14, 'NotImpact' union all
select '20110601', 15, 'NotImpact' union all
select '20110601', 16, 'NotImpact' union all
select '20110601', 17, 'NotImpact' union all
select '20110601', 18, 'NotImpact' union all
select '20110601', 19, 'NotImpact' union all
select '20110601', 20, 'NotImpact' union all
select '20110601', 21, 'NotImpact' union all
select '20110601', 22, 'NotImpact' union all
select '20110601', 23, 'NotImpact' union all
select '20110601', 24, 'NotImpact';
with cte as
(
select [Date], Period, [Event],
ROW_NUMBER() OVER (ORDER BY [Date], Period) AS rn
from @T
)
SELECT
T1.[Date], T1.Period,
ISNULL(T2.[Event], T1.[Event]),
T1.rn , T2.rn
FROM
cte T1
LEFT JOIN
cte T2 ON T1.rn BETWEEN T2.rn AND T2.rn + 12 AND T2.[Event] = 'Impact'