Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何添加日期范围之前的金额?_Sql_Sql Server_Tsql_Date_Select - Fatal编程技术网

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

Sql 如何添加日期范围之前的金额?,sql,sql-server,tsql,date,select,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

我正在运行此查询,它返回特定帐户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')
这很好,但我想让它做一件我不能做的额外事情,我想它还应该显示给定日期范围之前存在的所有金额的总和,例如,我将2014-01-28作为开始日期,它显示罚款,但运行金额列应该显示该日期之前的金额总和(期初金额),例如2014-01-27,在第一行的RunningAmount列和Amount列中应显示0,然后与我正在做的相同,即添加到上一个金额

e、 g

注:(第一行的
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   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
试试这个:

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')

添加具有预期输出的示例数据。
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
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')
| 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 |