1

下面的查询当前将总计显示为逐项,但我想要的是计算采购订单中所有项目的小计。

谢谢

输出应该是:

POID Item ItemQTY ItemPrice  ItemTotal   SubTotal
1      A       1     15.00       15.00      80.50
1      B       1     25.50       25.50      80.50
1      C       2     20.00       40.00      80.50
2      X       6      5.00       30.00      50.00
2      Y       2     10.00       20.00      50.00

关系purchase_order 1 - N purchase_order_items

SELECT
purchase_order.id AS POID,

purchase_order_items.description AS Item,
purchase_order_items.quantity AS ItemQTY,
purchase_order_items.price AS ItemPrice,
(purchase_order_items.quantity*purchase_order_items.price) AS ItemTotal

/* Here, Subtotal should be calculated and displayed */

FROM purchase_order
INNER JOIN purchase_order_items ON purchase_order.id = purchase_order_items.fk_purchase_order

我查看了MySQL 查找小计小计和 SQL,但无法应用于我的查询。

4

3 回答 3

3

您可以使用该WITH ROLLUP功能获取小计:

SELECT
purchase_order.id AS POID,

purchase_order_items.description AS Item,
purchase_order_items.quantity AS ItemQTY,
purchase_order_items.price AS ItemPrice,
SUM(purchase_order_items.quantity*purchase_order_items.price) AS ItemTotal
FROM purchase_order
INNER JOIN purchase_order_items ON purchase_order.id = purchase_order_items.fk_purchase_order
GROUP BY POID, Item WITH ROLLUP

这将创建一个包含Item = NULLPO 小计和POID = NULL总计的结果集。这些小计和总计进入ItemTotal这些行的列。

于 2013-06-27T09:53:34.013 回答
1

尝试

SELECT i.fk_purchase_order POID,
       description         Item,
       quantity            ItemQTY,
       price               ItemPrice,
       quantity * price    ItemTotal,
       s.subtotal          SubTotal
  FROM purchase_order_items i JOIN
(
  SELECT fk_purchase_order, SUM(quantity * price) subtotal
    FROM purchase_order_items
   GROUP BY fk_purchase_order
) s ON i.fk_purchase_order = s.fk_purchase_order

输出:

| 点 | 项目 | 项目 | 项目价格 | 项目总数 | 小计 |
-------------------------------------------------- ----------
| 1 | 一个 | 1 | 15 | 15 | 80.5 |
| 1 | 乙| 1 | 25.5 | 25.5 | 80.5 |
| 1 | C | 2 | 20 | 40 | 80.5 |
| 2 | X | 6 | 5 | 30 | 50 |
| 2 | 是 | 2 | 10 | 20 | 50 |

这是SQLFiddle演示

于 2013-06-27T09:47:34.630 回答
0

恐怕我认为最好的解决方案是交叉连接子选择: -

SELECT
purchase_order.id AS POID,
purchase_order_items.description AS Item,
purchase_order_items.quantity AS ItemQTY,
purchase_order_items.price AS ItemPrice,
(purchase_order_items.quantity*purchase_order_items.price) AS ItemTotal,
Sub1.FullTotal
FROM purchase_order
INNER JOIN purchase_order_items ON purchase_order.id = purchase_order_items.fk_purchase_order
CROSS JOIN 
(
    SELECT  SUM(purchase_order_items.quantity*purchase_order_items.price) AS FullTotal
    FROM purchase_order
    INNER JOIN purchase_order_items ON purchase_order.id = purchase_order_items.fk_purchase_order
) Sub1

将其带回分组采购订单 ID

SELECT
purchase_order.id AS POID,
purchase_order_items.description AS Item,
purchase_order_items.quantity AS ItemQTY,
purchase_order_items.price AS ItemPrice,
(purchase_order_items.quantity*purchase_order_items.price) AS ItemTotal,
Sub1.FullTotal
FROM purchase_order
INNER JOIN purchase_order_items ON purchase_order.id = purchase_order_items.fk_purchase_order
INNER JOIN 
(
    SELECT purchase_order.id, SUM(purchase_order_items.quantity*purchase_order_items.price GROUP BY purchase_order.id) AS FullTotal
    FROM purchase_order
    INNER JOIN purchase_order_items ON purchase_order.id = purchase_order_items.fk_purchase_order
) Sub1
ON purchase_order.id = Sub1.id
于 2013-06-27T09:34:21.203 回答