SQLTeam.com | Weblogs | Forums

I need query


#1

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


#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?


#3

how to get the total_inst and total_due columns list


#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


#5

Thanks Mangal