2010-10-06 50 views
0

基本上,我有以下查詢:mysql中的一列(它本身就是一個總和)的總和?

SELECT 

sr.name AS salesrepname, 
ct.name AS customertypename, 
c.name AS customername, 
c.code, 
p.description AS productname, 
p.unitofmeasure, 
SUM(col.vatableprice) AS totalsales, 
SUM(col.vatprice) AS vat, 
SUM(col.quantity) AS totalitems, 
SUM(col.quantity * col.costprice) AS totalsalecost, 
SUM(col.vatableprice) - SUM(col.quantity * col.costprice) AS totalprofit , 


(SELECT SUM(col2.vatableprice) AS totalsales FROM customerorders AS co2 
LEFT JOIN customerorderlines AS col2 ON col2.customerorder_id = co2.id 
LEFT JOIN customers AS c2 ON c2.id = co2.customer_id 
LEFT JOIN locations AS l2 ON l2.id = c2.location_id 
LEFT JOIN customertypes AS ct2 ON ct2.id = c2.customertype_id 
LEFT JOIN salesreps AS sr2 ON sr2.id = c2.salesrep_id 
LEFT JOIN products AS p2 ON p2.id = col2.product_id 
LEFT JOIN suppliers AS s2 ON s2.id = p2.supplier_id 
WHERE c.id = c2.id AND co2.type = 2 
AND c2.salesrep_id = 20 AND p2.supplier_id = 67 
AND co2.orderdate >= '2010-01-01 00:00:00' 
AND co2.orderdate <= '2010-08-31 23:59:59') AS credits , 

(SELECT SUM(col2.vatprice) AS totalvat FROM customerorders AS co2 
LEFT JOIN customerorderlines AS col2 ON col2.customerorder_id = co2.id 
LEFT JOIN customers AS c2 ON c2.id = co2.customer_id 
LEFT JOIN locations AS l2 ON l2.id = c2.location_id 
LEFT JOIN customertypes AS ct2 ON ct2.id = c2.customertype_id 
LEFT JOIN salesreps AS sr2 ON sr2.id = c2.salesrep_id 
LEFT JOIN products AS p2 ON p2.id = col2.product_id 
LEFT JOIN suppliers AS s2 ON s2.id = p2.supplier_id 
WHERE c.id = c2.id AND co2.type = 2 
AND c2.salesrep_id = 20 
AND p2.supplier_id = 67 
AND co2.orderdate >= '2010-01-01 00:00:00' 
AND co2.orderdate <= '2010-08-31 23:59:59') AS creditsvat , 

(SELECT SUM(col2.quantity * col2.costprice) AS totalcreditcost 
FROM customerorders AS co2 
LEFT JOIN customerorderlines AS col2 ON col2.customerorder_id = co2.id 
LEFT JOIN customers AS c2 ON c2.id = co2.customer_id 
LEFT JOIN locations AS l2 ON l2.id = c2.location_id 
LEFT JOIN customertypes AS ct2 ON ct2.id = c2.customertype_id 
LEFT JOIN salesreps AS sr2 ON sr2.id = c2.salesrep_id 
LEFT JOIN products AS p2 ON p2.id = col2.product_id 
LEFT JOIN suppliers AS s2 ON s2.id = p2.supplier_id 
WHERE c.id = c2.id AND co2.type = 2 
AND c2.salesrep_id = 20 
AND p2.supplier_id = 67 
AND co2.orderdate >= '2010-01-01 00:00:00' 
AND co2.orderdate <= '2010-08-31 23:59:59') 
AS totalcreditcost 

FROM customerorders AS co 
LEFT JOIN customerorderlines AS col ON col.customerorder_id = co.id 
LEFT JOIN customers AS c ON c.id = co.customer_id 
LEFT JOIN locations AS l ON l.id = c.location_id 
LEFT JOIN customertypes AS ct ON ct.id = c.customertype_id 
LEFT JOIN salesreps AS sr ON sr.id = c.salesrep_id 
LEFT JOIN products AS p ON p.id = col.product_id 
LEFT JOIN suppliers AS s ON s.id = p.supplier_id 
WHERE co.status_v = 5 
AND co.type = 1 
AND c.salesrep_id = 20 AND p.supplier_id = 67 
AND co.orderdate >= '2010-01-01 00:00:00' 
AND co.orderdate <= '2010-08-31 23:59:59' 
GROUP BY c.id 
ORDER BY customername 

我想實現的是場別名TOTALITEMS,這本身就是col.quantity的總和的總和。

請讓我知道你是否想要更多的細節。

在此先感謝。

回答

1

你可以使用INTO關鍵字的第一個表保存到一個臨時表。然後執行 SUM(totalitems),就像您在查詢的其餘部分所做的一樣。

+0

經過一番玩這個似乎對我來說是最好的選擇,謝謝。 – prevailrob 2010-10-07 10:50:08

2
+0

乾杯。注意到我不能在ORDER中使用ROLLUP,所以需要重新考慮我的輸出。另外,ROLLUP只是添加了總計的另一行,我不能從我的頭頂上弄清楚如何將它從我的輸出中分離出來(數據使用php進入表格) – prevailrob 2010-10-06 18:31:29

+0

您可以使用GROUP BY命令數據也。 – MatTheCat 2010-10-06 21:24:49