[英]Account balance: how to calculate it properly on SQL
我正在开发一个应用程序,我必须在其中存储一些银行帐户信息,包括每日帐户余额。
因此,例如:
17/10/2014 - (+) - Starting/Initial balance - 5,000.00
17/10/2014 - (=) - Balance - 5,000.00
-
18/10/2014 - (-) - Payment - (1,000.00)
18/10/2014 - (=) - Balance - 4,000.00
-
19/10/2014 - (=) - Balance - 4,000.00
-
20/10/2014 - (-) - Payment - (1,000.00)
20/10/2014 - (=) - Balance - 3,000.00
我想我可以创建一个特定的“account_balance”表,我可以在其中存储每天的每个帐户余额。
如果我错了,你能帮我找到最好的方法吗? 但是,如果我是对的,如何让数据库计算每日余额,特别是当用户开始编辑旧值时如何使数据库更新余额?
我所说的“旧价值观”是指:
1 - 这是“帐户 A”语句的样子:
18/10/2014 - (+) - Starting/Initial balance - 5,000.00
18/10/2014 - (=) - Balance - 5,000.00
-
19/10/2014 - (=) - Balance - 5,000.00
-
20/10/2014 - (=) - Balance - 5,000.00
2 - 但是用户忘记登记收入,所以他通过添加新收入来完成(所以现在必须更新余额):
18/10/2014 - (+) - Starting/Initial balance - 5,000.00
18/10/2014 - (+) - Sales commission - 2,500.00 <- USER ADDED THIS.
18/10/2014 - (=) - Balance - 7,500.00 <- THIS BALANCE HAS BEEN UPDATED.
-
19/10/2014 - (=) - Balance - 7,500.00 <- THIS BALANCE HAS BEEN UPDATED.
-
20/10/2014 - (=) - Balance - 7,500.00 <- THIS BALANCE HAS BEEN UPDATED.
而不是存储余额,有一个表,只为每个用户存储交易。
例如:
Date Transactions Comment
17/10/2014 +5,000.00 Starting/Initial balance -
18/10/2014 -1,000.00 Payment
20/10/2014 -1,000.00 Payment
然后你可以创建一个平衡视图(类似于):
create view balance as
select userId, sum(transactions) as balance from TransactionTable group by userId
如果您想更精确并包含开始和停止日期(即:能够在任何时间点获得平衡),您可以创建一个参数化视图(尚未尝试使用日期,但我认为它会起作用同样)。
此答案适用于 PostgreSQL,OP 在对原始问题的评论中询问了该答案。
数据完整性对我来说是最重要的。 所以我不愿意存储聚合值,除非 a) 性能很差,并且 b) dbms 可以保证聚合值是正确的。
我从这张桌子开始。
create table transactions (
trans_id serial primary key,
cust_id integer not null, -- foreign key, references customers, not shown
trans_time timestamp not null default current_timestamp,
trans_amt numeric(14,2) not null
);
create index on transactions (cust_id);
我选择了时间戳而不是日期,因为像这样的应用程序通常需要支持时间戳,而且在一般情况下,它的性能应该比日期差。 如果我们在时间戳方面获得了良好的性能,那么我们应该能够在日期方面获得良好的性能。 我没有假设单个客户的时间戳是唯一的。
我将 2000 万行随机数据加载到这个表中,然后我更新了统计信息。 数据包括正数和负数。 为了使目视检查更容易,金额甚至高达数百美元。
此类应用程序中更常见的查询之一涉及返回单个客户的登记簿——所有交易都有运行余额。
这是前三天客户 128 的原始数据。
cust_id trans_time trans_amt -- 128 2014-01-01 08:36:09 200.00 128 2014-01-01 14:18:10 200.00 128 2014-01-01 14:26:56 0.00 128 2014-01-01 18:17:31 400.00 128 2014-01-01 20:18:53 100.00 128 2014-01-02 00:10:35 0.00 128 2014-01-02 01:44:26 300.00 128 2014-01-02 15:49:31 -300.00 128 2014-01-03 00:33:23 400.00 128 2014-01-03 11:55:13 -200.00 128 2014-01-03 11:56:34 -100.00 128 2014-01-03 14:58:42 -400.00 128 2014-01-03 17:31:11 0.00
我们应该期待前三天的这些款项。
2014-01-01 900.00 2014-01-02 0.00 2014-01-03 -300.00
前三天的运行余额应该是这样的。
2014-01-01 900.00 2014-01-02 900.00 2014-01-03 600.00
每日余额登记表
select
cust_id
, trans_date
, sum(daily_amt) over (partition by cust_id order by trans_date) daily_balance
from (select
cust_id
, trans_time::date trans_date
, sum(trans_amt) daily_amt
from transactions
where cust_id = 128
group by cust_id, trans_date) x
order by cust_id, trans_date;
cust_id trans_date daily_balance -- 128 2014-01-01 900.00 128 2014-01-02 900.00 128 2014-01-03 600.00 . . .
寄存器的执行计划
执行计划显示上面的查询运行时间为 12 毫秒。 我认为这对于此类应用程序是合理的,但我可以通过索引表达式 ( trans_time::date
) 或复合索引将运行时间减少到 12 毫秒以下。
"WindowAgg (cost=7232.14..7252.94 rows=1040 width=40) (actual time=11.728..12.093 rows=294 loops=1)" " -> Sort (cost=7232.14..7234.74 rows=1040 width=40) (actual time=11.700..11.733 rows=294 loops=1)" " Sort Key: transactions.cust_id, ((transactions.trans_time)::date)" " Sort Method: quicksort Memory: 38kB" " -> HashAggregate (cost=7156.62..7169.62 rows=1040 width=16) (actual time=11.392..11.466 rows=294 loops=1)" " -> Bitmap Heap Scan on transactions (cost=39.66..7141.89 rows=1964 width=16) (actual time=0.839..9.753 rows=1961 loops=1)" " Recheck Cond: (cust_id = 128)" " -> Bitmap Index Scan on transactions_cust_id_idx (cost=0.00..39.17 rows=1964 width=0) (actual time=0.501..0.501 rows=1961 loops=1)" " Index Cond: (cust_id = 128)" "Total runtime: 12.272 ms"
在我的情况下,下面是表架构
为此,我提供以下解决方案
SELECT id, user_id, credit, debit,
COALESCE(((SELECT SUM(credit) FROM user_transactions b WHERE b.id <= a.id AND user_id = '7') - (SELECT SUM(debit) FROM user_transactions b WHERE b.id <= a.id AND user_id = '7')), 0) as balance
FROM user_transactions a WHERE user_id = '7' ORDER BY id ASC;
这是结果希望它会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.