计算MySQL中组记录的总和

时间:2016-04-28 13:55:54

标签: mysql select join sum

我有这个订单表

| ORDER_ID | PRODUCT | CUSTOMER  | QTY | DATE
---------------------------------------------
|       1  |  shoes  |    Nick   |   1 | 01/01/2016
|       2  |  shirts |    Nick   |   5 | 02/02/2016
|       3  |  shoes  |    Paul   |  10 | 03/03/2016
|       4  |  shirts |    Paul   |  20 | 04/04/2016

那么,如何使用ONE Select Statement实现此报告结果?

| Date_of_Order |  Customer  |  Quantity | PRODUCT_TOTAL_SALES |
-----------------------------------------------------------------
|   01/01/2016  |   Nick     |     1     |      shoes : 11     |  
|   02/02/2016  |   Nick     |    10     |     shirts : 25     |
|   03/03/2016  |   Paul     |     5     |      shoes : 11     |
|   04/04/2016  |   Paul     |    20     |     shirts : 25     |

我知道如何使用concat(column1, ' ', column2)创建合并列,但我没有成功为其中的分组项添加总和。当我尝试使用左连接时,我得到了产品的总和...但它总是全部总和而且它与订单的日期无关,所以当我尝试在我的查询中过滤结果一段时间后我仍然得到11件鞋子和25件衬衫......

2 个答案:

答案 0 :(得分:0)

您可以group by多个列并获得最小组的总和。

如果您想要每日销售,则代替GROUP BY product使用GROUP BY product, date

SELECT
    o.`date` AS Date_of_Order,
    SUM(o.qty) as Total_Quantity,
    CONCAT(o.product, ':', SUM(o.qty))
FROM
    orders o
GROUP BY product, `date`
ORDER BY `date`

答案 1 :(得分:0)

同一个表中的简单附加SELECT o.`date` AS Date_of_Order, o.Customer, o.qty as Quantity, (SELECT CONCAT(oo.product, ':', SUM(oo.qty)) FROM orders oo WHERE oo.product = o.product ) PRODUCT_TOTAL_SALES FROM orders o 可以在整个期间内完成:

+---------------+----------+----------+---------------------+
| Date_of_Order | Customer | Quantity | PRODUCT_TOTAL_SALES |
+---------------+----------+----------+---------------------+
| 01/01/2016    | Nick     |        1 | shoes:11            |
| 02/02/2016    | Nick     |        5 | shirts:25           |
| 03/03/2016    | Paul     |       10 | shoes:11            |
| 04/04/2016    | Paul     |       20 | shirts:25           |
+---------------+----------+----------+---------------------+
4 rows in set

输出:

SELECT
    o.`date` AS Date_of_Order,
    o.Customer,
    o.qty as Quantity,
    (SELECT
            CONCAT(oo.product, ':', sum(oo.qty))
        FROM
            orders oo
        WHERE
            oo.product = o.product
            AND STR_TO_DATE(oo.`date`,'%d/%m/%Y') BETWEEN '2016-01-01' AND '2016-03-03'
    ) PRODUCT_TOTAL_SALES
FROM
    orders o
WHERE 
    STR_TO_DATE(o.`date`,'%d/%m/%Y') BETWEEN '2016-01-01' AND '2016-03-03'

如果您想按特定时段过滤,则必须将其包含在以下两者中:

+---------------+----------+----------+---------------------+
| Date_of_Order | customer | Quantity | PRODUCT_TOTAL_SALES |
+---------------+----------+----------+---------------------+
| 01/01/2016    | Nick     |        1 | shoes:11            |
| 02/02/2016    | Nick     |        5 | shirts:5            |
| 03/03/2016    | Paul     |       10 | shoes:11            |
+---------------+----------+----------+---------------------+
3 rows in set

输出:

$ awk 'BEGIN{flag=0;count=0}NR%16==2{flag=1;print;next}flag{print;++count}count==3{count=0;flag=0}' <filename>