使用触发器/函数计算 postgresql 的利息
我目前正在开发一个简单的银行应用程序。
我已经构建了一个 postgresql 数据库,具有正确的表和函数。
我的问题是,我不知道如何计算账户利率。我有一个函数可以告诉我一次的余额。
如果我们说我们有 1 个月的期限,我想计算账户的利息。余额如下所示:
February Balance
1. $1000
3. $300
10. $700
27. $500
Balance on end of month: $500
我最初的想法是创建一个 for 循环,从该月的第一天循环到该月的最后一天,并连续添加该特定日期赚取的利息。
我想在月底使用的函数应该类似于addInterest(startDate,endDate,accountNumber),它应该在表中插入一行,添加赚取的利率。
有人可以带我走上正轨,或者向我展示一些有关 PL/PGSQL 的优秀学习资源吗?
编辑
我已经阅读了一些关于光标的内容。我应该使用光标浏览表格吗?
我发现使用游标有点令人困惑,这里有人有一些解释清楚的例子吗?
I'm currently working on a simple banking application.
I have built a postgresql database, with the right tables and functions.
My problem is, that I am not sure how to calculate the interest rate on the accounts. I have a function that will tell me the balance, on a time.
If we say that we have a 1 month period, where I want to calculate the interest on the account. The balance looks like this:
February Balance
1. $1000
3. $300
10. $700
27. $500
Balance on end of month: $500
My initial thoughts are to make a for loop, looping from the 1st in the month, to the last day in month, and adding the interest earned for that particular day in a row.
The function I want to use at end of month should be something like addInterest(startDate,endDate,accountNumber)
, which should insert one row into the table, adding the earned rate.
Could someone bring me on the right track, or show me some good learning resources on PL/PGSQL?
Edit
I have been reading a bit on cursors. Should I use a cursor to walk through the table?
I find it a bit confusing to use cursors, anyone here with some well explained examples?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
银行系统的利息计算方式有多种。
利息 = 余额 x 利率 x 天/年
余额类型
利率类型
日期/时间表类型
年份公式
结论
应将计算利息作为一项常规任务。最好的方法是根据个人帐户的频率设置按计划运行。
There are various ways of interest calculation in banking system.
Interest = Balance x Rate x Days / Year
Types of Balances
Types of Rates
Types of Days/Schedules
Year Formula
Conclusion
Interest should be calculated as a routine task. Best approach would be that would run on a schedule depending upon the frequency setup of individual accounts.
手册有一个部分关于循环和循环查询结果。还有用pl/pgsql编写的触发器函数的例子。该手册非常完整,是我所知道的最好的来源。
The manual has a section about loops and looping through query results. There are also examples of trigger functions written in pl/pgsql. The manual is very complete, it's the best source I know of.