Please refer to the diagram. This is done in Excel with SumIffs but I want to do this in SQL. The Value Expected should be calculated per DateTime based on a condition. The condition is that for the current row, look to either previous and/or next rows if the Time is with in 24 hours. In the example, for the first row, the DateTime column shows that the 2nd and 3rd DateTime values are within 24 hours of the 1st. So, I take 2+1+3 = 6 in the Value Expected column. For the 3rd row, (12/29/16 9am), I see the previous and next rows if they are with in 24 hours of (12/29/16 9am). In this case it would be the previous 3 row and the next 1 row. So I add 2+1+3+5 to get Value expected. If the 5th and/or 6th row were with in 24 hours, I would have included them for the total. Can Anybody suggest a simple way of doing this in SQL? I have tried Lag() and Lead() functions so far and isn't giving me what i want. I appreciate any help. Thanks.