[英]How to do sum of sum in mysql query
SELECT a.id AS supplier, sum( processed_weight ) AS total_qty
FROM supplier_inward a
INNER JOIN warehouseb ON a.id = b.supplier
WHERE a.master_product_id = '38'
GROUP BY b.supplier
輸出存在
supplier total_qty
12046 475.00
12482 99.00
輸出需要
total_qty
574.00
這里我需要這個查詢中的總和(total_qty)? 如何實現這一目標
只需修改GROUP BY
,添加WITH ROLLUP
:
SELECT a.id AS supplier, sum( processed_weight ) AS total_qty
FROM supplier_inward a
INNER JOIN warehouseb ON a.id = b.supplier
WHERE a.master_product_id = '38'
GROUP BY b.supplier
WITH ROLLUP
輸出:
supplier total_qty
12046 475.00
12482 99.00
NULL 574.00
這個怎么樣:
SELECT SUM(iQuery.total_qty) as iTotal
FROM
(SELECT a.id AS supplier, sum( processed_weight ) AS total_qty
FROM supplier_inward a
INNER JOIN warehouseb ON a.id = b.supplier
WHERE a.master_product_id = '38'
GROUP BY b.supplier) as iQuery
嘗試
SELECT sum( processed_weight ) AS total_qty
FROM supplier_inward a
INNER JOIN warehouseb ON a.id = b.supplier
WHERE a.master_product_id = '38'
編輯2 - 來自OP的評論更改結果結構:
有關其他列,請嘗試:
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 totalq
FROM
(
SELECT
a.id AS supplier,
sum( processed_weight ) AS total_qty,
FROM supplier_inward a
INNER JOIN warehouseb ON a.id = b.supplier
WHERE a.master_product_id = '38'
GROUP BY b.supplier) AS X
對於其他行:
SELECT
a.id AS supplier,
sum( processed_weight ) AS total_qty
FROM supplier_inward a
INNER JOIN warehouseb ON a.id = b.supplier
WHERE a.master_product_id = '38'
GROUP BY b.supplier
UNION ALL
SELECT null, X.total_qty
FROM
(
SELECT sum( processed_weight ) AS total_qty
FROM supplier_inward a
INNER JOIN warehouseb ON a.id = b.supplier
WHERE a.master_product_id = '38' ) AS X
嘗試不使用組,因為你想要總結每一件事
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.