1 回答
TA贡献1842条经验 获得超21个赞
我通过实现一个子查询,对子查询进行求和,然后最终将其连接到主查询来解决这个问题:
$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'])
- 1 回答
- 0 关注
- 124 浏览
添加回答
举报