使用触发器/函数计算 postgresql 的利息

发布于 2024-10-19 04:52:24 字数 625 浏览 7 评论 0原文

我目前正在开发一个简单的银行应用程序。

我已经构建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明月松间行 2024-10-26 04:52:24

银行系统的利息计算方式有多种。

利息 = 余额 x 利率 x 天/年

余额类型

  • 定期汇总余额
  • 每日汇总余额

利率类型

  • 固定利率 动态利率(根据余额)
  • 动态利率(根据期限) )
  • 动态费率(根据时间表)

日期/时间表类型

  • 日终处理(一天)
  • 月末处理(一个月) 季度末处理
  • (三个月) 半月
  • 末处理(六个月)月)
  • 年终处理(一年)

年份公式

  • 一年可以由 365 或 366 天组成。
  • 您的用户可能想要覆盖一年中的天数,在您的应用程序中维护一个单独的年份变量属性。

结论

应将计算利息作为一项常规任务。最好的方法是根据个人帐户的频率设置按计划运行。

There are various ways of interest calculation in banking system.

Interest = Balance x Rate x Days / Year

Types of Balances

  • Periodical Aggregate Balance
  • Daily Aggregate Balance

Types of Rates

  • Fixed Rate Dynamic Rate (according to balance)
  • Dynamic Rate (according to term)
  • Dynamic Rate (according to schedule)

Types of Days/Schedules

  • End of Day Processing (One day)
  • End of Month Processing (One month)
  • End of Quarter Processing (Three months)
  • End of Half Processing (Six months)
  • End of Year Processing (One year)

Year Formula

  • A year could consist of 365 or 366 days.
  • Your user might want to override number of days in a year, maintain a separate year variable property in your application.

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.

挽你眉间 2024-10-26 04:52:24

手册有一个部分关于循环和循环查询结果。还有用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文