简体   繁体   English

mysql 在最后一行的多列中求和值的最佳方法

[英]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 1 1 100 100 1000 1000 900 900
car 2车 2 2 2 200 200 2000 2000 1800 1800
car 3 3号车 9 9 900 900 10000 10000 9100 9100
car 4 4号车 null null null null 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 1 1 100 100 1000 1000 900 900
car 2车 2 2 2 200 200 2000 2000 1800 1800
car 3 3号车 9 9 900 900 10000 10000 9100 9100
car 4 4号车 null null null null null null null null
null null 12 12 1200 1200 13000 13000 11800 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM