Here is something to get you started - this may not be the exact thing you are looking for in regards to the data moving without it going down to zero. The hardest thing about posting a solution for me and probably for many others who respond to questions on this forum, is creating some sample data that is easily consumed by a query. So if you post your question with sample table(s) and data like I created below, which someone can then copy and use to write and test a query, you would get faster and more accurate answers.create table #tmp(reading_datetime datetime, tank_shell int);insert into #tmp values('08/04/2013 08:04:00.000',855), ('08/04/2013 08:08:00.000',855),('08/04/2013 08:15:00.000',851 ),('08/04/2013 08:16:00.000',0),('08/04/2013 08:58:00.000',22),('08/04/2013 23:59:00.000',534), ('09/04/2013 00:00:00.000',534 ),('09/04/2013 13:03:00.000',1177),('09/04/2013 13:03:04.000',0),('09/04/2013 13:18:00.000',11),('09/04/2013 14:20:00.000',51),('09/04/2013 23:59:00.000',614 );with cte as(select row_number() over (partition by cast(reading_datetime as date) order by reading_datetime asc ) as FirstVal, row_number() over (partition by cast(reading_datetime as date) order by reading_datetime desc) as LastVal, cast(reading_datetime as date) as dt, tank_shellfrom #tmp)select a.dt, sum(case when b.tank_shell = 0 then a.tank_shell else 0 end) + sum(case when a.LastVal = 1 then a.tank_shell else 0 end) - sum(case when a.FirstVal = 1 then a.tank_shell else 0 end)from cte a left join cte b on b.FirstVal = a.FirstVal + 1 and a.dt = b.dtgroup by a.dt;drop table #tmp;