[英]Calculate running balance in mysql with view
My simple table named ass_material with this columns: 我的简单表ass_material包含以下各列:
mat_id mat_name supplier request stock_received date
1 alloy test 30 0 feb13
2 alloy test 30 10 feb14
3 alloy test 30 20 feb14
How can I generate it or calculate it as: It will also generate a 'complete' status when stock_balance and request matches. 我如何生成它或将其计算为:当stock_balance和request匹配时,它还将生成“完成”状态。
mat_id mat_name supplier request stock_received Stock_balance date status
1 alloy test 30 0 0 feb13
2 alloy test 30 10 10 feb14
3 alloy test 30 20 30 feb14 complete
here's my reference code from my other view: how can i tweak this into my desired output above? 这是从其他角度来看的参考代码:如何将其调整为上面所需的输出?
SELECT m.`mat_id`, m.`mat_name`, m.`request`, m.`stock_receieved`,
(select sum(stock_received) - sum(stock_received)
from material m2
where m2.mat_name = m.mat_name and
m2.mat_id <= m.mat_id
) as Stock_balance,
m.`date`,
s.`sup_name`
FROM `material` m
LEFT JOIN `supplier` s on s.sup_id = m.supplier_id
ORDER BY m.`mat_id` ASC;
If stock_balance
is just the sum of stock_received
, then use that in the subquery: 如果
stock_balance
是简单相加stock_received
,然后使用子查询:
SELECT m.`mat_id`, m.`mat_name`, m.`request`, m.`stock_receieved`,
(select sum(stock_received)
from material m2
where m2.mat_name = m.mat_name and
m2.mat_id <= m.mat_id
) as Stock_balance,
(case when (select sum(stock_received)
from material m2
where m2.mat_name = m.mat_name and
m2.mat_id <= m.mat_id
) = request
then 'Complete'
else 'Incomplete'
end) as status,
m.`date`,
s.`sup_name`
FROM `material` m LEFT JOIN
`supplier` s
on s.sup_id = m.supplier_id
ORDER BY m.`mat_id` ASC;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.