SELECT x.Value ,SUM((case when Op_Type_Id IN(1,3,5)
then Value when Op_Type_Id IN (2,4,6) then(0-Value) END)) OVER(ORDER BY x.[Date]
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW )
AS Balance
FROM [dbo].[aaa] x LEFT JOIN
LEFT OUTER JOIN dbo.Op_type ON x.Op_Type_Id = dbo.Op_type.ID
WHERE (x.[date]>'(start date)for example:'2016-01-01 00:00:00'' )
ORDER BY x.[Date]
i created tmp table with same columns value balance and date if you want you can have id too after i used this code for
insert
and after that i run select form TMP table and used
when [Date2]>=@Datest AND [Date2]<=@dateend ORDER BY [Date2]
date2 is TMP table date this code works for me fine