我有我的选择代码:
SELECT m.`genmat_id`,m.`mat_type`,m.`mat_name`, m.`category`, m.`size`, m.`req_qty`, m.`stock_received`, m.`stock_released`, m.`release_approval`,
(select sum(stock_received) - sum(stock_released)
from genmaterial m2
where m2.mat_name = m.mat_name and
m2.genmat_id <= m.genmat_id
) as Stock_balance,
(case when (select sum(stock_received)
from genmaterial m2
where m2.mat_name = m.mat_name and
m2.genmat_id <= m.genmat_id
) = req_qty
then 'Complete'
when req_qty = 0 then ''
else 'Incomplete'
end) as status,
m.`date`,
s.`sup_name`
FROM `genmaterial` m LEFT JOIN
`supplier` s
on s.sup_id = m.sup_id
ORDER BY m.`genmat_id` ASC
OUTPUT(忽略Full Textts列):
Full Texts genmat_id mat_type mat_name category size req_qty stock_received stock_released release_approval Stock_balance status date sup_name
Edit Delete 69 pur aluminum flat bar metal 100 m 130 0 0 0 Incomplete 2014-02-14 E-store
Edit Delete 70 Raw aluminum flat bar metal 100 m 130 50 0 50 Incomplete 2014-02-15 E-store
Edit Delete 71 Raw aluminum flat bar metal 100 m 130 80 0 130 Complete 2014-02-20 E-store
Edit Delete 72 pur mild steel metal 100 m 50 0 0 0 Incomplete 2014-02-13 Handy Shop
Edit Delete 73 Raw mild steel metal 100 m 50 50 0 50 Complete 2014-02-15 Handy Shop
Edit Delete 74 pur mild steel metal 100 m 40 0 0 50 Incomplete 2014-02-14 E-store
Edit Delete 77 Raw mild steel metal 100 m 40 40 0 90 Incomplete 2014-02-17 E-store
mat_id 74是由mat_type = pur
标识的新请求
并且mat_id 77是在stock_received和mat_type = Raw
上接收40的请求。现在,stock_balance恰到好处,因为它总结了material + the stock_received
之前的总和。
我的问题:如何让状态完整 有这种情况吗?
它会在柜台内mat_type=raw
时汇总stock_received
如果counter=req_qty
有status=complete
我现有的代码会在Stock_balance = req_qty
时生成状态。
我该怎么做counter = req_qty
而stock_balance是来自每个mat_name的计数器的总和
stock_balance - stock_released(stock_out)
喜欢(参见计数器专栏): 计数器只计算每个新mat_type = pur
的stock_receivedFull Texts genmat_id mat_type mat_name category size req_qty stock_received counter stock_released release_approval Stock_balance status date sup_name
Edit Delete 69 pur aluminum flat bar metal 100 m 130 0 0 0 0 Incomplete 2014-02-14 E-store
Edit Delete 70 Raw aluminum flat bar metal 100 m 130 50 50 0 50 Incomplete 2014-02-15 E-store
Edit Delete 71 Raw aluminum flat bar metal 100 m 130 80 130 0 130 Complete 2014-02-20 E-store
Edit Delete 72 pur mild steel metal 100 m 50 0 0 0 0 Incomplete 2014-02-13 Handy Shop
Edit Delete 73 Raw mild steel metal 100 m 50 50 50 0 50 Complete 2014-02-15 Handy Shop
Edit Delete 74 pur mild steel metal 100 m 40 0 0 0 50 Incomplete 2014-02-14 E-store
Edit Delete 77 Raw mild steel metal 100 m 40 40 40 0 90 Incomplete 2014-02-17 E-store