How to do sum of sum in mysql query
Just modify GROUP BY
, adding WITH ROLLUP
:
SELECT a.id AS supplier, sum( processed_weight ) AS total_qtyFROM supplier_inward a INNER JOIN warehouseb ON a.id = b.supplierWHERE a.master_product_id = '38'GROUP BY b.supplier WITH ROLLUP
Output:
supplier total_qty12046 475.0012482 99.00NULL 574.00
try
SELECT sum( processed_weight ) AS total_qtyFROM supplier_inward aINNER JOIN warehouseb ON a.id = b.supplierWHERE a.master_product_id = '38'
EDIT 2 - AFTER comment from OP changing the result structure:
For an additional column try:
SELECT X.supplier,X.total_qty,(SELECT sum( processed_weight ) FROM supplier_inward a INNER JOIN warehouseb ON a.id = b.supplier WHERE a.master_product_id = '38') AS totalqFROM(SELECT a.id AS supplier, sum( processed_weight ) AS total_qty, FROM supplier_inward aINNER JOIN warehouseb ON a.id = b.supplierWHERE a.master_product_id = '38'GROUP BY b.supplier) AS X
For an additonal row:
SELECT a.id AS supplier, sum( processed_weight ) AS total_qtyFROM supplier_inward aINNER JOIN warehouseb ON a.id = b.supplierWHERE a.master_product_id = '38'GROUP BY b.supplierUNION ALLSELECT null, X.total_qtyFROM( SELECT sum( processed_weight ) AS total_qtyFROM supplier_inward aINNER JOIN warehouseb ON a.id = b.supplierWHERE a.master_product_id = '38' ) AS X