简体   繁体   中英

how to SUM operation of multiple subqueries in laravel

I have 3 table called pencairan, induk_pencairan, turunan_belanja. now i want to showing total_order in another table but its related table,you can see this table like this:

pencairan

pencairan
+----+------------+------------+------------+
| id |  induk_id  | price      |   qty   
+----+------------+------------+------------+
|  1 |   1        |   1000     |    10   //  so from this i can create total 10000 from (price*qty)
+----+------------+------------+------------+
|  2 |   1        |   2000     |    5    // and this 10000
+----+------------+------------+------------+
|  3 |   2        |   5000     |    10   // and this 50000
+----+------------+------------+------------+
|  4 |   2        |   1000     |    10   // and this 10000
+----+------------+------------+------------+

induk_pencairan:

induk_pencairan
+----+------------+------------+------------+
| id |turunan_id  | some_data  |    some_data
+----+------------+------------+------------+
|  1 |   1        | some_data  |        -> this total is 20000 from pencairan table
+----+------------+------------+------------+
|  2 |   1        | some_data  |        -> total = 60000
+----+------------+------------+------------+
|  3 |   2        | some_data  |        -> total ....
+----+------------+------------+------------+
|  4 |   2        | some_data  |        -> total ....
+----+------------+------------+------------+

turunan_belanja:

turunan_belanja
+----+------------+------------+------------+
| id |    name    | balance    |  some data . . .
+----+------------+------------+------------+
|  1 |   phone    | 10000000   |      -> in here i want to show 80000 its total from induk_pencairan
+----+------------+------------+------------+
|  2 |     PC     | 50000000   |      -> . . . . .
+----+------------+------------+------------+
|  3 | Electronic | 500000000  |      -> . . .  .
+----+------------+------------+------------+

this 3 table is related,and now i want to show data like this:

turunan_belanja
+----+------------+------------+------------+
| id |  name      |  balance   |   total_order (its from table induk_pencairan)
+----+------------+------------+------------+
|  1 |   phone    | 10000000   |    80000
+----+------------+------------+------------+
|  2 |     PC     | 50000000   |    ......
+----+------------+------------+------------+
|  3 | Electronic | 50000000   |    . . .  .
+----+------------+------------+------------+

and its my relation

 Models IndukPencairan
    public function induk_pencairan()
    {
        return $this->belongsTo(\App\Models\IndukPencairan::class ,'turunan_id');
    }

 Models Pencairan
    public function pencairan()
    {
        return $this->belongsTo(\App\Models\Order::class ,'induk_id');
    }

so what i can do for this???

and my step now is just selecting on category_order like this:

 public function get_belanja()
    {
        $belanja = TurunanBelanja::where('status', 'active')->get();
        return response()->json($belanja ,200);
    }

UPDATE, i create this subquery like this, but on second query have error

     $belanja = DB::table("induk_pencairan")
    ->select("induk_pencairan.*",
              DB::raw("(SELECT SUM(pencairan.harga*qty) FROM pencairan
                          WHERE pencairan.indukpencairan_id = induk_pencairan.id
                          GROUP BY pencairan.indukpencairan_id) as product_stock"))
    ->get();

    $belanja2 = DB::table("turunan_belanja")
    ->select("turunan_belanja.*",
              DB::raw("(SELECT SUM(product_stock) FROM induk_pencairan
                          WHERE induk_pencairan.rek_id = turunan_belanja.id
                          GROUP BY induk_pencairan.rek_id) as total"))
    ->get();

Error is UNDEFINED COLUMN product_stock - how can I parse this product_stock from query 1 to query 2?

In pure SQL you can get your aggregated data in single query as

select t.id,
  t.name,
  t.balance,
  coalesce(sum(p.price * p.qty),0) total_order
from turunan_belanja t
left join induk_pencairan i on t.id  =i.turunan_id
left join pencairan p on i.id = p.induk_id
group by t.id,
  t.name,
  t.balance

DEMO

You can transform above query for laravel query builder as

$results = DB::table('turunan_belanja AS t')
    ->select([
    't.id',
    't.name',
    't.balance'
    DB::raw('coalesce(sum(p.price * p.qty),0) AS total_order')
    ])
    ->leftJoin('induk_pencairan AS i', 'i.turunan_id', '=', 't.id' )
    ->leftJoin('pencairan AS p', 'p.induk_id', '=', 'i.id')
    ->groupBy('t.id')
    ->groupBy('t.name')
    ->groupBy('t.balance')
    ->get();

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