在历史表中求和余额

时间:2016-01-07 17:36:03

标签: sql-server-2008

我需要帮助我无法达到预期的输出。我已经使用了如下所示的光标。我有两张桌子1.交易2.历史。我需要在Transaction表中为ID汇总金额,并将其插入历史表中的revised_amount列下,并将还原金额存储在Original_amount列中。以下是示例。谢谢

Tran_ID      ID          Amount
1066    Dhan Group       $4,800.00 
1327    Dhan Group       $2,670.00 
1329    Dhan Group       $1,800.00 
1330    Dhan Group       $1,290.00 
3953    Admin Group     0
2650    Admin Group     $364368.69
2651    Admin Group     $1604546.97

历史记录表中的所需输出

Hist_ID    ID         ORIGINAL_AMOUNT      REVISED_AMOUNT
1      Dhan Group                                $4,800.00 
2      Dhan Group         $4,800.00              $7,470.00 
3      Dhan Group         $7,470.00              $9,270.00 
4      Dhan Group         $9,270.00              $10,560.00 

5      Admin Group                               $0
6      Admin Group       $0                      $364,368.69 
7      Admin Group       $364,368.89             $1,968,915.86 
=============================================================

USE [LEM]
BEGIN
DECLARE @Proj_RID nvarchar(255),
@Revised_Amount decimal(12,2),
@Original_Amount decimal(12,2)

DECLARE Running_Total CURSOR FOR

Select [ID], [Amount]
FROM [Transactions]

OPEN Running_Total;

FETCH NEXT FROM Running_Total into @Proj_RID, @Original_Amount;
WHILE @@FETCH_STATUS = 0
BEGIN 
SET @Revised_Amount = @Revised_Amount+ @Original_Amount;

UPDATE HISTORY
SET REVISED_AMOUNT = @Revised_Amount,
Original_Amount =  @Revised_Amount - @Original_Amount
WHERE [Project_ID] = @Proj_RID

FETCH NEXT FROM Running_Total into  @Proj_RID, @Original_Amount;
END 
CLOSE Running_Total;
DEALLOCATE Running_Total;
END
========================================================

1 个答案:

答案 0 :(得分:1)

以下是如何实现这一目标的示例。请注意,您需要将某个排序列用作TransactionDate

;WITH cte AS(SELECT *, (SELECT SUM(amount) FROM Transactions
                        WHERE id = t.ID AND TranDate < t.TranDate) AS ORIGINAL_AMOUNT  
             FROM Transactions t)
SELECT ID, ORIGINAL_AMOUNT, Amount + ISNULL(ORIGINAL_AMOUNT, 0) AS REVISED_AMOUNT 
FROM cte

这是完整的脚本(但我使用事务ID作为排序列):

DECLARE @t TABLE
    (
      Tran_ID INT ,
      ID NVARCHAR(100) ,
      Amount MONEY
    )

INSERT  INTO @t
VALUES  ( 1066, 'Dhan Group', 4800.00 ),
        ( 1327, 'Dhan Group', 2670.00 ),
        ( 1329, 'Dhan Group', 1800.00 ),
        ( 1330, 'Dhan Group', 1290.00 ),
        ( 2649, 'Admin Group', 0 ),
        ( 2650, 'Admin Group', 364368.69 ),
        ( 2651, 'Admin Group', 1604546.97 )


;WITH cte AS(SELECT *, (SELECT SUM(amount) FROM @t
                        WHERE id = t.ID AND Tran_ID < t.Tran_ID) AS ORIGINAL_AMOUNT  
             FROM @t t)
SELECT ID, ORIGINAL_AMOUNT, Amount + ISNULL(ORIGINAL_AMOUNT, 0) AS REVISED_AMOUNT 
FROM cte

输出:

ID          ORIGINAL_AMOUNT REVISED_AMOUNT
Dhan Group  NULL            4800.00
Dhan Group  4800.00         7470.00
Dhan Group  7470.00         9270.00
Dhan Group  9270.00         10560.00
Admin Group NULL            0.00
Admin Group 0.00            364368.69
Admin Group 364368.69       1968915.66