I have below sql query to get summation of a column name amount , where the result will be grouped by month.
SELECT MONTHNAME( created ) as monthname, sum( amount ) as total FROM transactions GROUP BY monthname
Example, In transactions table I have an amount field where the amount is a numeric value field and transactions have created and modified fields.
So, we can write this query in query builder like below way
public function index() { $query = $this->Transactions->find(); $query->select([ ‘monthname’ => $query->func()->monthname([ ‘created’ => ‘identifier’ ]), ‘total’ => $query->func()->sum(‘Transactions.amount’) ]) ->group([‘month’]); }
After iteration we should get the result like below
monthname total
July 100
May 300
….. …..