简体   繁体   中英

sum incorrect after using join in Eloquent Laravel

I had to join two tables together and after some trial and error I was able to get it, but while trying to sum the joined field to later sort by that quantity I realized the numbers were much higher than what they are supposed to be. I tried to follow this question's solution but it didn't work for me: The sum amount from join tables is incorrect . Here is my query down below:

        $query = Item::has('backorders')
        ->join('SOP10200', 'IV00102.ITEMNMBR','=','SOP10200.ITEMNMBR')
        ->select('IV00102.ITEMNMBR',
            //These sums are wrong when using join
            Item::raw("SUM(IV00102.QTYONHND) as qty"),
            Item::raw("SUM(IV00102.QTYONORD) as ordered"),
            Item::raw("SUM( ( CASE WHEN IV00102.LOCNCODE LIKE 'IT-%' THEN IV00102.QTYONHND END ) ) as transit"),
            Item::raw("SUM(SOP10200.QUANTITY) as backorder"),
        )
        ->where('IV00102.PRIMVNDR', Auth::user()->vendor_id)
        ->groupBy('IV00102.ITEMNMBR')
        ->orderBy($group['field'], $group['sort'])
        ->limit(2147483647);

Here are my relationships:

public function item(){
        return $this->belongsTo(Item::class, 'ITEMNMBR', 'ITEMNMBR');
    }
public function backorders(){
        return $this->hasMany(Backorder::class, 'ITEMNMBR', 'ITEMNMBR')->where('SOPTYPE', 5);
    }

I'd like to note that I could not use the relational identifiers in the join and had to resort to referencing the tables directly, but the relationships work otherwise, and have been tested without the join. In other words the whole reason I am joining is so that I can sort by the backorders(SOP10200). if there is a more elegant solution to this I am all for it. To avoid repeated offered solutions I'd like to also attach my previous question related to and solving the join issue: Is there a way to select fields from an eager loaded table in Laravel?

I solved this by implementing a subquery, doing sum on the subquery and then finally joining it to the main query:

        $subQuery = Backorder::select('ITEMNMBR', Backorder::raw('sum(QUANTITY) as backorder'), 'SOPTYPE')
        ->groupBy('ITEMNMBR', 'SOPTYPE');
        $subQuerySql = $subQuery->toSql();

        $query = Item::has('backorders')
        ->where('IV00102.PRIMVNDR', Auth::user()->vendor_id)
        ->leftjoin(Item::raw('(' . $subQuerySql . ') as bbo'),function($join) use ($subQuery) {
            $join->on('IV00102.ITEMNMBR', '=', 'bbo.ITEMNMBR');
        })
        ->where('bbo.SOPTYPE', 5)
        ->select('IV00102.ITEMNMBR',
            'bbo.backorder',
            Item::raw("SUM(IV00102.QTYONHND) as qty"),
            Item::raw("SUM(IV00102.QTYONORD) as ordered"),
            Item::raw("SUM( ( CASE WHEN IV00102.LOCNCODE LIKE 'IT-%' THEN IV00102.QTYONHND END ) ) as transit"),
        )
        ->mergeBindings($subQuery->getQuery())
        ->groupBy('IV00102.ITEMNMBR', 'bbo.backorder')
        ->orderBy($group['field'], $group['sort'])

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