添加订单数量以给出每个产品订购的数量

时间:2017-08-31 14:55:33

标签: mysql sql

我有以下SQL,其中列出了我的所有订单行以及该订单行中产品的数量。

SELECT p.externalReference as KitNumber, 
       p.description as Kitname,
      (SELECT quantity 
         FROM order_line 
        WHERE id = ol.id) as KitQtyShipped
  FROM order_line ol 
  JOIN shipment s ON ol.shipmentId = s.id
  JOIN product p ON ol.productId = p.id
  JOIN order_item oi ON s.orderItemId = oi.id
 WHERE s.state = 'despatched' 
   AND ol.quantity != 0
 ORDER BY KitNumber DESC;

给出类似

的输出
+-----------+--------------------------+---------------+
| KitNumber | Kitname                  | KitQtyShipped |
+-----------+--------------------------+---------------+
| 269588    | product2                 |            30 |
| 269291    | product1                 |             3 |
| 269291    | product1                 |             1 |
| 269291    | product1                 |             2 |
| 269291    | product1                 |             3 |
| 269291    | product1                 |             3 |
| 269291    | product1                 |             2 |
| 269291    | product1                 |             1 |
| 269291    | product1                 |             4 |
| 269291    | product1                 |             1 |
| 269291    | product1                 |             2 |
| 269291    | product1                 |             1 |
| 269291    | product1                 |             2 |
| 269291    | product1                 |             1 |
| 269291    | product1                 |             4 |
| 269291    | product1                 |             1 |
| 269291    | product1                 |             2 |
| 269291    | product1                 |             2 |
| 269291    | product1                 |             1 |
| 269291    | product1                 |             2 |
| 269291    | product1                 |             1 |
| 269291    | product1                 |             2 |
| 269291    | product1                 |             1 |
| 269290    | product3                 |             2 |
| 269290    | product3                 |             3 |
| 269290    | product3                 |             2 |
| 269290    | product3                 |            10 |
| 269290    | product3                 |             3 |
| 269290    | product3                 |            26 |
| 269290    | product3                 |             1 |
| 269290    | product3                 |            11 |
| 269290    | product3                 |             5 |

我需要的是通过KitNumber对此进行分组,以便为​​每个工具包分配一行,其中包含kitQtyShipped数量的总计。即上面的所有行为每个工具包加在一起

我试过了:

SELECT p.externalReference as KitNumber, 
       p.description as Kitname,
       COUNT(*) * (SELECT quantity 
                     FROM order_line 
                    WHERE id = ol.id)  as KitQtyShipped
  FROM order_line ol 
  JOIN shipment s ON ol.shipmentId = s.id
  JOIN product p ON ol.productId = p.id
  JOIN order_item oi ON s.orderItemId = oi.id
 WHERE s.state = 'despatched' 
   AND ol.quantity != 0
 GROUP BY ol.productId
 ORDER BY KitNumber DESC;

但这并没有给出正确的价值

输出我希望得到类似的东西:

+-----------+--------------------------+--------------------------+
| KitNumber | Kitname                  | KitQtyShipped            |
+-----------+--------------------------+--------------------------+
| 269588    | product2                 |(Sum of all orderlines) 30|
| 269291    | product1                 |                       45 |
| 269290    | product3                 |                       63 |

3 个答案:

答案 0 :(得分:0)

我认为你需要:

SELECT p.externalReference as KitNumber, 
       p.description as Kitname,
       SUM(quantity) as KitQtyShipped
  FROM order_line ol 
  JOIN shipment s ON ol.shipmentId = s.id
  JOIN product p ON ol.productId = p.id
  JOIN order_item oi ON s.orderItemId = oi.id
 WHERE s.state = 'despatched' 
   -- AND ol.quantity != 0  -- no need this to SUM
 GROUP BY p.externalReference, p.description
 ORDER BY KitNumber DESC

但是再次没有源数据和预期的输出只是猜测。

我看到你不使用order_item。这也应该有效:

SELECT p.externalReference as KitNumber, 
       p.description as Kitname,
       SUM(quantity) as KitQtyShipped
  FROM order_line ol 
  JOIN shipment s ON ol.shipmentId = s.id
  JOIN product p ON ol.productId = p.id      
 WHERE s.state = 'despatched' 
 GROUP BY p.externalReference, p.description
 ORDER BY KitNumber DESC

答案 1 :(得分:0)

更容易设置为临时表,然后在那里求和。如果某些MYSQL语法不正确,请原谅我。

编辑:如果您正在查看KitShipped的总计,您希望使用SUM GROUP BY KitNumber而不是COUNT。

--Create a temp table for your SQL query
CREATE TEMPORARY TABLE IF NOT EXISTS SumTable AS 
 (SELECT 
 p.externalReference as KitNumber, 
 p.description as Kitname,
 (SELECT quantity from order_line WHERE id = ol.id)  as KitQtyShipped

 FROM order_line ol 
 JOIN shipment s ON ol.shipmentId = s.id
 JOIN product p ON ol.productId = p.id
 JOIN order_item oi ON s.orderItemId = oi.id
 WHERE s.state = 'despatched' AND ol.quantity != 0)

 --Do your sum here.

 Select Sum( KitQtyShipped) as TotalShipped, KitNumber from Sumtable
 Group by KitNumber

答案 2 :(得分:0)

我认为它可能比你预期的要容易(请检查一下,因为我无法测试它,因为我还没有创建表脚本): 我刚刚在你的第一个查询中添加了SUM,GROUP BY:

SELECT p.externalReference as KitNumber, 
       p.description as Kitname,
       SUM(ol.quantity) as KitQtyShipped
  FROM order_line ol 
  JOIN shipment s ON ol.shipmentId = s.id
  JOIN product p ON ol.productId = p.id
  JOIN order_item oi ON s.orderItemId = oi.id      
 WHERE s.state = 'despatched' 
   AND ol.quantity != 0
 GROUP BY p.externalReference, p.description
 ORDER BY KitNumber DESC;