My simple table named ass_material with this columns:
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.
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:
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.