Your statement is calculating [duration] for each row using only facts contained in that row.Here is your sample data and your statement with a rowNumber added for clarity:declare @ETL_Log table (ProcCategory varchar(10), ProcName varchar(20), Notes varchar(20), LogTS datetime)insert @ETL_Logselect 'Staging ', 'Table 1', 'Stage Table 1', '2009/08/25 1:30:01' union allselect 'Load', 'Master Load', 'Load Complete', '2009/08/25 3:00:00'Select row_number() over (order by LogTS) as rowNumber ,ProcCategory ,ProcName ,Notes ,LogTS ,Datediff(Minute , Case when ProcCategory = 'Load' and Notes = 'Load Complete' then LogTS else 0 end, LogTS) as DurationFrom @ETL_LogOUTPUT:rowNumber ProcCategory ProcName Notes LogTS Duration-------------------- ------------ -------------------- -------------------- ----------------------- -----------1 Staging Table 1 Stage Table 1 2009-08-25 01:30:01.000 576692102 Load Master Load Load Complete 2009-08-25 03:00:00.000 0
RowNumber 1 is calculated based only on facts in rowNumber 1. And rowNumber 2 is calculated based only on facts in rowNumber 2. If you want to calculate the difference in time between rowNumber 2 and rowNumber 1 then we need a way to "associate" these two rows out of all the other rows in the table. I see no common attributes in the sample data to make that correlation.Be One with the OptimizerTG