简体   繁体   中英

sum column based on another column laravel

I want to sum column AMOUNT, and the result is fill the column BALANCE. if column TYPE is DEBIT, then it will sum.. but if column TYPE is KREDIT, then it will be minus. this is the table table image

to create that table, i used this query in laravel controller :

$get_result        =  DB::select( DB::raw("SELECT statement.created_at, statement.descript, statement.amount, statement.sign, statement.reference 
                                                    FROM statement,lender 
                                                    WHERE statement.created_at BETWEEN DATE_ADD(' $date_from ',INTERVAL 1 DAY) 
                                                    AND '$date_to' 
                                                    AND statement.lender_id = lender.id 
                                                    AND lender.user_id= $userId ") );

and I used this code in view to display data :

<thead>
                                <tr class="txtcenter">
                                    <th class="all">Date </th>
                                    <th class="all">Description </th>
                                    <th class="all">Amount</th>
                                    <th class="all">Type</th>
                                    <th class="all">Reference</th>
                                    <th class="all">Balance</th>
                                </tr>
                            </thead>
                            <tbody>@foreach($get_result as $statement)
                                    <tr class="accordion">
                                        <td>{{SUBSTR($statement->created_at,0,10) }}</div>
                                        <td>{{$statement->descript}} </div>
                                        <td>IDR {{number_format($statement->amount, 0, '.', ',')}}  </div> 
                                        <td>{{ $statement->sign }}  </div>
                                        <td>{{ $statement->reference }} </div>
                                        <td>  </div>
                                    </tr>@endforeach
                            </tbody>

I do not know how to make the proper code to calculate it all. Please help me. Thanks in advance

Try this query

SELECT statement.created_at, statement.descript, statement.amount, statement.sign, statement.reference,
(sum(case when  statement.descript = 'Credit' then statement.amount else 0 end) -
sum(case when  statement.descript = 'Debit' then statement.amount else 0 end)) balance
  FROM statement,lender  WHERE statement.created_at BETWEEN DATE_ADD(' $date_from ',INTERVAL 1 DAY)  AND '$date_to' AND statement.lender_id = lender.id  AND lender.user_id= $userId

The "classical" way for this (it is a special case of a running sum) is to use a correlated query:

SELECT statement.created_at, statement.descript, statement.amount, statement.REFERENCE,
    ( SELECT 
      SUM(CASE WHEN t2.descript = 'Debit'  THEN t2.amount 
               WHEN t2.descript = 'Credit' THEN -t2.amount ELSE 0 END ) 
      FROM statement t2
      WHERE t2.created_at <= statement.created_at
    ) AS Balance
FROM statement
ORDER BY statement.created_at ;

or use a sql variable like in this:

SET @csum := 0;
SELECT statement.created_at, statement.descript, statement.amount,
       statement.REFERENCE,
    (CASE WHEN statement.descript = 'Debit' THEN
            (@csum := @csum + statement.amount)  
          WHEN statement.descript = 'Credit' THEN 
            (@csum := @csum - statement.amount)  
         ELSE  0  
     END) as Balance
FROM statement
ORDER BY statement.created_at ;

Check the sqlFiddle for a working code - not including the lender join and your other where criteria yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM