在销售和购买数量之间找出每行的余额数量

时间:2017-10-17 12:15:11

标签: mysql balance

我需要帮助才能在MySql查询中找出减去销售数量后的产品数量余额。这是我的疑问:

SELECT 
eng_stock.doc_id, 
eng_vouchers_detail.prod_id, 
GREATEST(
eng_stock.purch_date, 
eng_stock.sale_date
) AS vouch_date, 
eng_vouchers_detail.purch_qty, 
eng_vouchers_detail.sale_qty, 
GREATEST(
eng_stock.supplier, 
eng_stock.customer
) AS cust_sup, 
eng_stock.report_link 
FROM eng_stock 
LEFT JOIN
eng_vouchers_detail 
ON eng_stock.doc_id = eng_vouchers_detail.doc_id
WHERE eng_vouchers_detail.prod_id = 43
ORDER BY vouch_date

and here is result of this query

请帮我解决此问题

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT
eng_stock.doc_id,
eng_vouchers_detail.prod_id,
GREATEST(
    eng_stock.purch_date,
    eng_stock.sale_date
) AS vouch_date,
eng_vouchers_detail.purch_qty,
eng_vouchers_detail.sale_qty,
eng_stock.report_link,
sum(eng_vouchers_detail.purch_qty) AS p_sum,
sum(eng_vouchers_detail.sale_qty) AS s_sum
FROM
    eng_stock
LEFT JOIN eng_vouchers_detail ON eng_stock.doc_id = eng_vouchers_detail.doc_id
    eng_vouchers_detail.prod_id = 43
WHERE
GROUP BY eng_stock.doc_id
ORDER BY
    vouch_date

如果您现在需要,请提供表格的一些示例数据。