samedi 14 mars 2020

Sum of columns with item and item type grouping

I have table with

seller_id, transaction_type, sub_total, total_commission_fee, refund_fee, cancellation_fee
3 transaction_type
payment, cancel, refund

i want to get the sum of sub_total, total_commission_fee, refund_fee, cancellation_fee for each seller_id

sample
seller_id   transaction_type    sub_total   total_commission_fee    refund_fee  cancellation_fee
3           order               40          0                       0           0
4           order               10          0                       0           0   
3           cancel              0           0                       0           3   
3           refund              28          0                       2           0

i want result like this

seller_id   payment_total(sum of all sub_total transaction_type order)  cancel_total(sum of all cancellation_fee transaction_type cancel)   refund_total (sum of all refund_fee transaction_type refund)

i can get total without transaction type.

Transaction::groupBy('seller_id')
  ->selectRaw('sum(sub_total) as total, seller_id')

Is there a way to get result as i want. Else i have to do a get request and loop through each one. This may cause problem when the table becomes big

And what is this kind of operations called?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire