I have a table with different types of data. I want to find
sum(typeA) - sum(typeB)
Type A = whereIn('type', ['CreditNote','ReceiptVoucher'])
Type B = ->whereIn('type', ['DebitNote','PaymentVoucher'])
This is the query I came up with, but it isn't working. What am I missing?
Transaction::where('contact_id',$this->contact->id)->where('date', '<', $from)
->where(function ($query) {
$query
->whereIn('type', ['CreditNote','ReceiptVoucher'])
->selectRaw("SUM(amount) as in");
})
->Where(function ($query) {
$query
->whereIn('type', ['DebitNote','PaymentVoucher'])
->selectRaw("SUM(amount) as out");
})
->selectRaw("COALESCE(SUM(in),0) - COALESCE(SUM(out),0) as amount")
->groupBy('contact_id')
->get();
If I can't find a difference like this, I could also just get the two sums and I can subtract them later.