如何添加日期范围之前的金额?

时间:2014-12-01 05:13:18

标签: sql sql-server tsql date select

我正在运行此查询,它会返回特定帐户ID和日期范围的起始余额加上运行余额(除了以前的余额)。

SELECT 
    Payments.Accounts.AccountID, Payments.Accounts.AccountTitle, 
    Payments.Transactions.DateTime as TranasactionDateTime, 
    Payments.Transactions.Amount,
    SUM(Payments.Transactions.Amount) OVER (PARTITION BY Payments.Transactions.Account_Id 
                                            ORDER BY Payments.Transactions.DateTime
                                            rows between unbounded preceding and current row) as RunningAmount     
FROM 
    Payments.Accounts 
INNER JOIN
    Payments.Transactions ON Payments.Accounts.AccountID = Payments.Transactions.Account_ID
WHERE
    Payments.Transactions.Account_ID = 1  
    AND Payments.Transactions.DateTime >= Convert(smalldatetime, '2014-01-28')
    AND Payments.Transactions.DateTime <= CONVERT(smalldatetime, '2014-12-28')

输出:

AccountID   AccountTitle    TranasactionDateTime    Amount  RunningAmount
-------------------------------------------------------------------------
1           Test Account     2014-01-28 09:21:00    200.00   200.00
1           Test Account     2014-10-28 09:23:00    800.00  1000.00

很好,但是我想让它再做一件我无法做的事情,我希望它还应该显示给定日期范围之前存在的所有金额的总和,例如我是通过2014-01-28作为开始日期,它显示罚款,但运行金额列应显示该日期之前的金额总和(开仓金额),例如2014-01-27,在第一行的RunningAmount列和Amount列中应显示0然后与我正在做的相同,即添加到之前的金额。

e.g。

AccountID   AccountTitle    TranasactionDateTime    Amount  RunningAmount
-------------------------------------------------------------------------
1           Test Account      (all old dates)         0       120.00 
1           Test Account     2014-01-28 09:21:00    200.00    320.00
1           Test Account     2014-10-28 09:23:00    800.00   1120.00

注意:(第一行&#39; s RunningAmount是给定日期范围之前存在的金额总和)

更新了查询:

SELECT 
    A.AccountID, A.AccountTitle, '' AS TranasactionDateTime, 
    0 AS Amount, 
    SUM(T.Amount) AS RunningAmount 
FROM
    Payments.Accounts A 
INNER JOIN 
    Payments.Transactions T ON A.AccountID = T.Account_ID
WHERE 
    T.Account_ID = 1 
    AND A.DateTime < CONVERT(smalldatetime, '2014-10-28')
GROUP BY 
    A.AccountID, A.AccountTitle 

UNION 

SELECT 
    A.AccountID, A.AccountTitle, A.TranasactionDateTime, 
    A.Amount, A.RunningAmount 
FROM 
    (SELECT 
         A.AccountID, A.AccountTitle, T.DateTime AS TranasactionDateTime, 
         T.Amount, 
         SUM(T.Amount) OVER (PARTITION BY T.Account_Id 
                             ORDER BY T.DateTime 
                             ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS RunningAmount     
     FROM 
         Payments.Accounts A 
     INNER JOIN 
         Payments.Transactions T ON A.AccountID = T.Account_ID
     WHERE 
         T.Account_ID = 1) AS A 
WHERE 
    A.TranasactionDateTime BETWEEN CONVERT(smalldatetime, '2014-10-28') 
                               AND CONVERT(smalldatetime, '2014-12-29')

结果:

AccountID   AccountTitle    TranasactionDateTime    Amount  RunningAmount
-------------------------------------------------------------------------
1            Test Account   2014-10-28 09:23:00     800.00  1100.00
1            Test Account   2014-12-28 09:12:00     500.00  1600.00

但我想要像这样输出

AccountID   AccountTitle    TranasactionDateTime    Amount  RunningAmount
-------------------------------------------------------------------------
1            Test Account   OLDDates-All              0      300.00      (=sum of old amounts)
1            Test Account   2014-10-28 09:23:00     800.00  1100.00     (=800+300)
1            Test Account   2014-12-28 09:12:00     500.00  1600.00     (=1100+500)

交易表中的实际数据:

TransasctionID  Issuance_ID  DateTime            Account_ID  Description        Amount  User_ID
1003              NULL       2014-01-28 09:21:00    1        money transfered   200.00  0
1005              NULL       2014-02-28 09:23:00    1        money transfered   100.00  0
3                 NULL       2014-10-28 09:23:00    1        money transfered   800.00  0
2                 NULL       2014-12-28 09:12:00    1        money transfered   500.00  0

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT A.AccountID, A.AccountTitle, NULL AS TranasactionDateTime, 0 AS Amount, 
       SUM(T.Amount) AS RunningAmount 
FROM Payments.Accounts A 
INNER JOIN Payments.Transactions T ON A.AccountID = T.Account_ID
WHERE T.Account_ID = 1 AND T.DateTime < CONVERT(smalldatetime,'2014-01-28')
GROUP BY A.AccountID, A.AccountTitle 
UNION 
SELECT A.AccountID, A.AccountTitle, A.TranasactionDateTime, A.Amount, A.RunningAmount 
FROM (SELECT A.AccountID, A.AccountTitle, T.DateTime AS TranasactionDateTime, T.Amount, 
             SUM(T.Amount) OVER (PARTITION BY T.Account_Id ORDER BY T.DateTime ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS RunningAmount     
      FROM Payments.Accounts A 
      INNER JOIN Payments.Transactions T ON A.AccountID = T.Account_ID
      WHERE T.Account_ID = 1 
     ) AS A 
WHERE A.TranasactionDateTime BETWEEN CONVERT(smalldatetime,'2014-01-28') AND CONVERT(smalldatetime,'2014-12-28')

检查SQL FIDDLE DEMO

<强>输出

| ACCOUNTID | ACCOUNTTITLE |            TRANASACTIONDATETIME | AMOUNT | RUNNINGAMOUNT |
|-----------|--------------|---------------------------------|--------|---------------|
|         1 | Test Account |                          (null) |      0 |           300 |
|         1 | Test Account |  October, 28 2014 09:23:00+0000 |    800 |          1100 |
|         1 | Test Account | December, 28 2014 09:12:00+0000 |    500 |          1600 |