mysql - Yii2 activeRecord multiple sums from multiple join tables -


is possible, activerecord query builder multiple sums when joining multiple tables?

the query looks this:

        $po = purchaseorder::find() //todo groupby multiple sums possible?             ->select([                 'po.*',                 'sum(pi.invoice_units) invoicedunits',                 'sum(ar.received_units) receivedarticles'             ])             ->from('purchase_order po')             ->joinwith('invoice')             ->joinwith('articlesreceived')             ->where(['po.id' => 3960])             ->groupby(['po.id'])             ->one(); 

the result expeting :

sum(pi.invoice_units) = 234 + 2 = 236; sum(ar.received_units) = 10 + 6 = 16; 

however this: 236*2 = 472; 16*2 = 32;

is possible multiple sums way? in normal sql, join , sum inside join, how in activerecord?

this correct query returns right sums:

select po.*, ars.receivedarticles, pis.invoicedunits purchase_order po     left join (select ar.purchase_order_id, sum(ar.received_units) receivedarticles purchase_articles_received ar group ar.purchase_order_id) ars on po.id = ars.purchase_order_id     left join (select pi.order_id, sum(pi.invoice_units) invoicedunits purchase_invoice pi group pi.order_id) pis on po.id = pis.order_id     po.id = 3960 group po.id; 


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -