table name is person
id month inst
1 jan 2000
2 feb 2000
3 march 2000
my table structure is like i want expected output as below
id month inst total_inst due_amount
1 jan 2000 6000 4000
2 feb 2000 6000 2000
3 march 2000 6000 null
djj55
November 4, 2015, 12:01pm
2
What is your level of T-SQL experience? What have you tried?
Is this a running total problem?
How are Total_inst and Due_amount defined?
how to get the total_inst and total_due columns list
Mangal
November 4, 2015, 1:26pm
4
You can do something like this:
SELECT M.*, B.SUM_INST AS TOTAL, SUM_INST - SUM(INST)OVER(ORDER BY ID ) AS DUE
FROM @T AS M
CROSS APPLY
(SELECT SUM(A.INST) AS SUM_INST
FROM @t AS A
) AS B
See, how to calculate the running totals in SQL Server