【问题标题】:Sum of sub query result with group by laravel eloquentLaravel eloquent 分组子查询结果的总和
【发布时间】:2018-03-12 19:30:39
【问题描述】:

我有两张表,一张transactions 和另一张transaction_charges 每笔交易可能有多个交易费用意味着一个有很多关系。现在我需要的是获取一个月/年/周发生的交易。如果我可以在查询本身内部执行 group by 而不是对模型集合执行循环会更好。

到目前为止,我已经编写了代码来获取交易的总费用

 $defaults = collect([]);
    /** @var Builder $transactions */
    $transactions = app(TransactionServices::class)
        ->getTransactions($defaults->merge($options))
        ->has('charges')->has('operation')->selectSub(function ($query){
            /** @var Builder $query */
            $query->selectRaw('SUM(amount)')->from('transaction_charges tc')
                ->whereRaw('tc.transaction_id = transaction.transaction_id');
        }, 'totalCharge');

但要获得每月结果,我需要进行分组,但我认为分组不会从子查询中获取正确的聚合结果,因为这里我需要的是一个月内 totalCharge 的总和/周/组。那么我如何才能获得费用总计为每月/每年组的交易

【问题讨论】:

  • 只写一个原始查询。如果您需要将结果作为 eloquent 集合,请使用 hydrateRaw()
  • 如果您添加表/模型结构并解释您的用例,将会有所帮助。
  • 有两个表transactionstransaction_charges 并且transactionstransaction_charges 之间存在has many 关系。我需要的是获取一个按“月/年/日”分组的transactions 模型集合,这些模型具有额外的模型属性,这些属性代表该组相关交易的总交易费用的总和
  • @PaulSpiegel 实际上也没有针对上述情况获得原始查询解决方案。

标签: php mysql laravel


【解决方案1】:

我不太了解您的系统是如何工作的,但我确实想了解它并为您的问题想一个可能的解决方案。
我尽力解释代码。我希望你明白英语不是我的母语。

// I suppose that TransactionServices represents "transaction" Table and has transaction_charges
$transactions = TransactionServices::has('charges')->get(); // If I understand well your code that returns the transactions that has charges.
// $transactions = TransactionServices::has('charges')->has('operation')->get(); // that returns the transactions that has charges AND operation.

// the month to filter the charges
$month = 'march'; // selected month can be dynamic this is an example

// Get all TransactionServices with total amount of charges
$transactionsWithTotalCharge = $transactions->each(function ($transaction, $key) {
    $totalAmount = $transaction->charges->where('month', $month)->sum('amount'); // This make the sum of all charges of march for that transaction 
    $transaction->put('chargesTotalAmount' , $totalAmount); // This add chargesTotalAmount to every TransactionServices
    return $transaction;
});

// You can verify if the sum is correct
foreach($transactionsWithTotalCharge as $transaction) {
    // dd($transaction); 
    dd($transaction->chargesTotalAmount); 
}

我无法对此进行测试,希望能成功

【讨论】:

  • 实际上在这里,您在查询所有可能的结果后自己创建了一个循环,而没有在查询本身中应用 groupBy。我需要的是在查询本身中应用 groupBy,然后返回交易记录以及特定月/周/年的费用总和
  • 您需要分组TransactionServicescharges?在这里,我获取所有具有chargesTransactionServices,然后过滤每个TransactionServicescharges,其中与特定月份匹配并为所有它们求和
  • 当我做->where('month', $month)你可以使用whereBetween
  • 有两个表transactionstransaction_charges 并且transactionstransaction_charges 之间存在has many 关系。我需要的是获取按“月/年/日”分组的transactions 模型集合,其中包含额外的模型属性,这些属性代表该组相关交易的总交易费用的总和
  • 当添加 ->get() 时,我们得到了没有任何“分组依据”过滤器的查询结果集合我需要在查询中应用分组依据
猜你喜欢
  • 1970-01-01
  • 2019-12-24
  • 1970-01-01
  • 2020-09-24
  • 2018-10-22
  • 2021-10-26
  • 2021-05-28
  • 1970-01-01
  • 2019-12-06
相关资源
最近更新 更多