The table in the link below:
Тhe Result
column which is the result of a T-SQL
query in which each record reflects the movement of a чproduct-modelч from an чemployeeч to another through a bill: if the amount
is moved from the employee
, the amount will be filled in the Amount_Out
column and vise versa.
For example: rows 3 and 4 show that 40 pieces of the product hp
moved from emp1
to emp3
through the bill with code 111
. This movement reduces the total amount of this product for emp1
to be 60
, and increases the total amount of the same product for emp3
to be 40
.
Now I need to calculate in the query the result in each record to show the total amount of a product in an employee's custody until the date in the record:
Result = The amount calculated for all previous bills + Amount_In - Amount_Out
which means the result will depend on previous records' values. How could this be achieved by the query in SQL server 2008? and which is the best to achieve it, stored procedure or view?