【发布时间】:2015-01-29 02:03:26
【问题描述】:
我正在运行此查询,它返回给我特定帐户 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,然后与我所做的相同,即添加到以前的金额。
例如
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
注意:(第一行的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
【问题讨论】:
-
添加具有预期输出的示例数据。
标签: sql sql-server tsql date select