Bạn đang tìm giải pháp tính tổng tích lũy:
Kiểm tra tại đây: http://rextester.com/SEZS4911
select if (@last_order <> sales_order, @acm_order := order_quantity - bill_qty, @acm_order := @acm_order - bill_qty) as Open,
sales_order,
so_create_on,
order_quantity,
bill_item,
bill_qty,
bill_doc,
bill_doc_date,
@last_order := sales_order
from
(select @last_order := 0, @acm_order := 0) x,
(SELECT sa.sales_order,
sa.so_create_on,
sa.order_quantity,
sh.bill_item,
sh.bill_qty,
sh.bill_doc,
sh.bill_doc_date
FROM sales_order sa
LEFT JOIN shipped_order sh ON sa.sales_order = sh.sales_order
order by sa.sales_order, sh.bill_doc_date) y
;