EMfinanga
1 month ago
122
2
Laravel

How can I query multiple SUM from the same table using where condition?

Posted 1 month ago by EMfinanga

i tried this but got the error

                     $balanceIn=CashSales::with('items')
                       ->select('item_id',
                        DB::raw('sum(quantity)  as tqty whereIn(l_type,["credit_sale,cash_sale"])'),
                        DB::raw('sum(quantity)  as  tqty1 whereIn( l_type , ["open balance,purchase"]'))
                    ->groupby('item_id')
                    ->get();

error

               Illuminate\Database\QueryException
             SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check 
                  the manual that corresponds to your MariaDB server version for the right syntax to use near 
            'whereIn(l_type,["credit_sale,cash_sale"]), sum(quantity) as tqty1 whereIn( l_t' at line 1 (SQL: select 
                      `item_id`, sum(quantity) as tqty whereIn(l_type,["credit_sale,cash_sale"]), sum(quantity) as tqty1 
               whereIn( l_type , ["open balance,purchase"] from `cash_sales` group by `item_id`)

Please sign in or create an account to participate in this conversation.