I have an SQL query which should return the Average cost price from the table.
I have a few joins because I need to pull certain statuses from the order header.
See eloquent query.
return $avgsell = DB::table('STK_STOCK')
->join('ORD_DETAIL','ORD_DETAIL.OD_ORDER_NUMBER','=','ORD_HEADER.OH_ORDER_NUMBER')
->where('ORD_HEADER','ORD_DETAIL.OD_STOCK_CODE','=',$code)->where('ORD_HEADER.OH_PRIORITY','<>',4)->where('ORD_DETAIL.OD_QTYORD','<>','0')
->avg('ORD_DETAIL.OD_UNITCST');
I get the following error
SQLSTATE[42000]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near 'ORD_HEADER'. (SQL: select avg([ORD_DETAIL].[OD_UNITCST]) as aggregate from [STK_STOCK] inner join [ORD_DETAIL] on [ORD_DETAIL].[OD_ORDER_NUMBER] = [ORD_HEADER].[OH_ORDER_NUMBER] where 0005-20-02-01 [ORD_HEADER] = ORD_DETAIL.OD_STOCK_CODE [ORD_HEADER].[OH_PRIORITY] <> 4 and [ORD_DETAIL].[OD_QTYORD] <> 0)