下面的查询目前按项目显示总数,但我想要的是计算采购订单中所有项目的小计。
由于
输出应为:
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 finding subtotals和Subtotals and SQL,但无法应用于我的查询。
答案 0 :(得分:2)
您可以使用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
这将创建一个结果集,其中PO小计为Item = NULL
,总计为POID = NULL
。这些小计和总计将位于这些行的ItemTotal
列中。
答案 1 :(得分: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
输出:
| POID | ITEM | ITEMQTY | ITEMPRICE | ITEMTOTAL | SUBTOTAL | ------------------------------------------------------------ | 1 | A | 1 | 15 | 15 | 80.5 | | 1 | B | 1 | 25.5 | 25.5 | 80.5 | | 1 | C | 2 | 20 | 40 | 80.5 | | 2 | X | 6 | 5 | 30 | 50 | | 2 | Y | 2 | 10 | 20 | 50 |
这是 SQLFiddle 演示
答案 2 :(得分: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