Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts

I have the below table result:
PayDate     PayAmount     Client_ID
------------------------------------
2012-06-14     2000          1
2012-06-15     2000          1
2012-06-16     2000          1
2012-06-15     4500          2
2012-06-16     4500          2
2012-06-17     4500          2

I need the below result:
PayDate     PayAmount      Sum
-------------------------------------
2012-06-14     2000        2000
2012-06-15     2000        4000
2012-06-16     2000        6000
2012-06-15     4500        4500
2012-06-16     4500        9000
2012-06-17     4500        13500
Posted
Updated 19-Dec-13 0:21am
v3

Please, have a look at example:
SQL
DECLARE @tmp TABLE (PayDate DATETIME, PayAmount DECIMAL(8,2), Client_ID INT)

INSERT INTO @tmp (PayDate, PayAmount, Client_ID)
VALUES('2012-06-14', 2000.00, 1), ('2012-06-15', 2000.00, 1), ('2012-06-16', 2000.00, 1),
('2012-06-15', 4500.00, 2), ('2012-06-16', 4500.00, 2), ('2012-06-17', 4500.00, 2)


SELECT t1.RowNo AS OperationID, t1.Client_ID, t1.PayDate, t1.PayAmount, SUM(t2.PayAmount) AS Total
FROM (
    SELECT ROW_NUMBER() OVER(PARTITION BY Client_ID ORDER BY PayDate) AS RowNo, Client_ID, PayDate, PayAmount
    FROM @tmp
    ) AS t1 INNER JOIN (
        SELECT ROW_NUMBER() OVER(PARTITION BY Client_ID ORDER BY PayDate) AS RowNo, Client_ID, PayDate, PayAmount
        FROM @tmp
            ) AS t2 ON t1.Client_ID = t2.Client_ID AND t1.RowNo >= t2.RowNo
--uncomment below line to add condition
--WHERE t1.Client_ID = 1
GROUP BY t1.Client_ID, t1.RowNo, t1.PayDate, t1.PayAmount


Result:
OperationID Client_ID   PayDate                    PayAmount  Total
1           1           2012-06-14 00:00:00.000    2000.00    2000.00
2           1           2012-06-15 00:00:00.000    2000.00    4000.00
3           1           2012-06-16 00:00:00.000    2000.00    6000.00
1           2           2012-06-15 00:00:00.000    4500.00    4500.00
2           2           2012-06-16 00:00:00.000    4500.00    9000.00
3           2           2012-06-17 00:00:00.000    4500.00    13500.00


Is that what you want?
 
Share this answer
 
Try looking here: Calculating simple running totals in SQL Server[^] - it suggests several ways to do this.
 
Share this answer
 
Comments
Maciej Los 19-Dec-13 4:04am    
+5
Meysam Toluie 19-Dec-13 4:32am    
Please read the question and the sample of result carefully.
Pay attention to Client_ID column and Sum Column.
Please try following , I am not tested but may be this will correct .


SQL
Select A.PayDate,
          A.PayAmount,
  (Select  sum(PayAmount) as [Sum]
            From Teflan.dbo.LoanInstallment as B
              Where A.PayDate < B.PayDate
          )
  From Teflan.dbo.LoanInstallment As A
  Where Client_ID = 1
  Order By A.PayDate
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900