How to Improve this query please:
SELECT FatherID, Date, Line, ProductCode, StockCode, QtyIn, QtyOut,
(SELECT SUM(QtyIn - QtyOut) AS Expr1
FROM (SELECT ROW_NUMBER() OVER (ORDER BY Date, ID) AS Line, *
FROM Test1) AS t1
WHERE (Line <= t0.Line) AND (ProductCode = t0.ProductCode) AND (StockCode = t0.StockCode)) AS total, Type
FROM dbo.test2 AS t0
-----------------
test2 is a view:
SELECT ROW_NUMBER() OVER (ORDER BY Date, ID) AS Line, *FROM Test1