ActiveRecord sum 和 group by 对带有 sum 和 group by 的子查询进行求和和分组

发布于 2024-12-20 23:13:02 字数 546 浏览 1 评论 0原文

我试图弄清楚如何在 Ruby ActiveRecord 中对执行第一级求和和分组的子查询进行求和和分组。

我可以使用命名范围和后续的选择函数来获取子查询的总和。它在一个循环中执行,该表为每笔付款构建一个包含一行的表,每个现金预付款组的顶部都有付款总额,以及所有辛迪加现金预付款的每个辛迪加组的总计:

@funder
  .cashadvance_syndicators(@start_date, @end_date)
  .select("sum(amount) as amount, percent_syndicated, management_fee, residual_commission")
  .where(['cashadvance_syndicators.iso_id = ?', cs.iso_id])
  .group('cashadvance_syndicators.id')

其中资助者拥有许多现金预付款,向借款人提供许多付款,其中许多现金预付款银团根据整个贷款的百分比参与其中。

我需要能够汇总为每个辛迪加者选择的列中的金额。

I am trying to figure out how to sum and group on a sub-query that does a first level sum and group, in Ruby ActiveRecord.

I can get the sums of the sub-query using a named scope and a subsequent select function. That gets executed in a loop that builds a table with a row for each payment, with payment totals on the top of each cashadvance group, and totals for each syndicator group for all the syndicators cashadvances:

@funder
  .cashadvance_syndicators(@start_date, @end_date)
  .select("sum(amount) as amount, percent_syndicated, management_fee, residual_commission")
  .where(['cashadvance_syndicators.iso_id = ?', cs.iso_id])
  .group('cashadvance_syndicators.id')

where a funder has many cashadvances, which has many payments to borrowers in which many cashadvance syndicators participate in based on a percent of the whole loan.

I need to be able to sum the amounts in the columns selected for each syndicator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文