[英]mysql best way to sum values in multiple column in the last row
I have a table:我有一张桌子:
Product Category![]() |
Number Purchased![]() |
Total Discount $![]() |
Total Price![]() |
Total Sales![]() |
---|---|---|---|---|
car 1![]() |
1 ![]() |
100 ![]() |
1000 ![]() |
900 ![]() |
car 2![]() |
2 ![]() |
200 ![]() |
2000 ![]() |
1800 ![]() |
car 3 ![]() |
9 ![]() |
900 ![]() |
10000 ![]() |
9100 ![]() |
car 4 ![]() |
null ![]() |
null ![]() |
null ![]() |
null ![]() |
SELECT
categ.category_name AS 'Product Category',
SUM(order.quantity) AS 'Number Purchased',
SUM(order.discount) AS 'Total Discount $',
SUM(order.price) AS 'Total Price',
(SUM(products.price) - SUM(order.discount)) AS 'Total Sales'
FROM
products
LEFT JOIN
orders USING (product_id)
LEFT JOIN
categ USING (category_id)
GROUP BY
categ.category_id
ORDER BY
categ.category_name
I'm trying to figure out the best way to produce the sum of the columns in the last row, demonstrated below:我试图找出产生最后一行中列总和的最佳方法,如下所示:
Product Category![]() |
Number Purchased![]() |
Total Discount $![]() |
Total Price![]() |
Total Sales![]() |
---|---|---|---|---|
car 1![]() |
1 ![]() |
100 ![]() |
1000 ![]() |
900 ![]() |
car 2![]() |
2 ![]() |
200 ![]() |
2000 ![]() |
1800 ![]() |
car 3 ![]() |
9 ![]() |
900 ![]() |
10000 ![]() |
9100 ![]() |
car 4 ![]() |
null ![]() |
null ![]() |
null ![]() |
null ![]() |
null ![]() |
12 ![]() |
1200 ![]() |
13000 ![]() |
11800 ![]() |
so far I've tried:到目前为止,我已经尝试过:
SELECT
categories.category_name AS 'Product Category',
SUM(order_items.quantity) AS 'Number Purchased',
SUM(order_items.discount_amount) AS 'Total Discount $',
SUM(order_items.item_price) AS 'Total Price',
(SUM(order_items.item_price) - SUM(order_items.discount_amount)) AS 'Total Sales'
FROM
products
LEFT JOIN
orders USING (product_id)
LEFT JOIN
categories USING (category_id)
GROUP BY
categories.category_id
UNION ALL
SELECT
NULL,
SUM('Number Purchased'),
SUM('Total Discount $'),
SUM('Total Price'),
SUM('Total Sales')
FROM
products
LEFT JOIN
orders USING (product_id)
LEFT JOIN
categ USING (category_id)
GROUP BY
categ.category_id
ORDER BY
categ.category_name
doesn't seems to be working, any suggestions?似乎没有用,有什么建议吗?
You can do that programatically in your application, that might be the fastest implementation.您可以在您的应用程序中以编程方式执行此操作,这可能是最快的实现。 If you really want to aggregate in MySQL (which executes a second sub-query) and do
UNION ALL
, then just fix the second part.如果您真的想在 MySQL (执行第二个子查询)中聚合并执行
UNION ALL
,那么只需修复第二部分。
You don't need to GROUP BY
there:您不需要
GROUP BY
那里:
SELECT
categories.category_name AS 'Product Category',
SUM(order_items.quantity) AS 'Number Purchased',
SUM(order_items.discount_amount) AS 'Total Discount $',
SUM(order_items.item_price) AS 'Total Price',
(SUM(order_items.item_price) - SUM(order_items.discount_amount)) AS 'Total Sales'
FROM
products
LEFT JOIN
orders USING (product_id)
LEFT JOIN
categories USING (category_id)
GROUP BY
categories.category_id
ORDER BY
categ.category_name
UNION ALL
SELECT
NULL,
SUM(order_items.quantity),
SUM(order_items.discount_amount),
SUM(order_items.item_price),
SUM(order_items.item_price) - SUM(order_items.discount_amount)
products
LEFT JOIN
orders USING (product_id)
LEFT JOIN
categ USING (category_id)
Using UNION
does not enforce the sorting order by the ORDER BY
clause unfortunately.不幸的是,使用
UNION
不会强制执行ORDER BY
子句的排序顺序。 Try using with rollup
in the group by clause:尝试在 group by 子句中使用
with rollup
:
SELECT
categ.category_name AS 'Product Category',
SUM(order.quantity) AS 'Number Purchased',
SUM(order.discount) AS 'Total Discount $',
SUM(order.price) AS 'Total Price',
(SUM(products.price) - SUM(order.discount)) AS 'Total Sales'
FROM
products
LEFT JOIN
orders USING (product_id)
LEFT JOIN
categ USING (category_id)
GROUP BY
categ.category_id with rollup
ORDER BY
categ.category_name
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.