我有两个表,一个是event_invoice和payments.Here event_invoice有发票行,因为付款表将包含多个单行发票行.Below是event_invoice结构
id Eventcode Currency Amount
1 E032 INR 375400
2 E032 INR 366740
3 E032 INR 283250
4 E034 USD 5000
5 E034 USD 5500
6 E034 USD 5000
7 E034 USD 5500
这是付款结构
invoice_id rcvamount
1 20000
2 50000
2 50000
3 40000
5 1500
6 5000
我的左侧查询如下
DB::table('event_invoice')
->leftjoin('payments', 'event_invoice.Id', '=', 'payments.invoice_id')->where('event_invoice.Status','=','1')
->where( DB::raw('year(DueDate)'), $year)
->select(DB::raw('
sum(event_invoice.Amount) as invoicevalue,
sum(payments.recieved_amount+payments.adjust_amount) as recvamount
'))
->groupBy('event_invoice.EventName')->groupBy('event_invoice.CurrencyType')->distinct()
->get();
结果:
eventcode currency invoicevalue
E032 INR 1392130
E034 USD 21500
但实际结果应如下所示
eventcode currency invoicevalue
E032 INR 1025390
E034 USD 21500
当我在支付表中进行左连接查询时,事件代码在支付表中有两行,因此它添加了两次特定发票以避免这种情况我使用了distinct()但它不起作用。 能帮我解决这个问题。