Here's one way:declare @cust_fcst table (file_date datetime, file_crq int)insert @cust_fcstselect '2009-03-25', 30500 union allselect '2009-04-01', 29000 union allselect '2009-04-08', 29000 union allselect '2009-04-15', 27000 union allselect '2009-04-22', 28500;with cteas(select file_date ,sum(file_crq) qty ,row_number() over (order by file_date) as rnfrom @cust_fcstgroup by file_date)select a.file_date ,a.qty ,a.qty-isNull(b.qty, a.qty) as changefrom cte aleft outer join cte b on b.rn = a.rn - 1OUTPUT:file_date qty change----------------------- ----------- -----------2009-03-25 00:00:00.000 30500 02009-04-01 00:00:00.000 29000 -15002009-04-08 00:00:00.000 29000 02009-04-15 00:00:00.000 27000 -20002009-04-22 00:00:00.000 28500 1500
Be One with the OptimizerTG