2016-09-09 62 views
0

早安MySQL的總和,並更新

我有一個MySQL表,它看起來是這樣的。

表:接收

+--------------+-----------+---------+---------+-------+ 
| DateReceived | ItemCode | QtyPack | QtyStan | PCS | 
+--------------+-----------+---------+---------+-------+ 
| 2016-09-09 | DRY000004 | 30.00 | 0.00 | 24.00 | 
| 2016-09-09 | DRY000007 | 60.00 | 0.00 | 12.00 | 
| 2016-09-09 | DRY000010 | 90.00 | 0.00 | 1.00 | 
| 2016-09-09 | DRY000011 | 100.00 | 0.00 | 1.00 | 
| 2016-09-09 | DRY000012 | 1.00 | 0.00 | 12.00 | 
| 2016-09-09 | DRY000012 | 1.00 | 0.00 | 12.00 | 
+--------------+-----------+---------+---------+-------+ 

和表方差

+-----------+-----------+ 
| ItemCode | Receiving | 
+-----------+-----------+ 
| DRY000004 |  NULL | 
| DRY000007 |  NULL | 
| DRY000010 |  NULL | 
| DRY000011 |  NULL | 
| DRY000012 |  NULL | 
| DRY000013 |  NULL | 
| DRY000014 |  NULL | 
| DRY000016 |  NULL | 
| DRY000017 |  NULL | 
| DRY000018 |  NULL | 
| DRY000019 |  NULL | 
| DRY000020 |  NULL | 
| DRY000021 |  NULL | 
| EQU000001 |  NULL | 
+-----------+-----------+ 

我的目標在這裏是在列表Receiving傳輸之前獲取的QtyPack * PCS + QtyStan的總和:Variance和這裏是我的代碼

UPDATE Variance VAR INNER JOIN receiving REv ON VAR.ItemCode = REV.ItemCode 
SET VAR.Receiving = replace(REV.QtyPack, ',', '') * replace(REV.PCS, ',', '') + replace(REV.QtyStan, ',', '') 
WHERE VAR.ItemCode = REV.ItemCode and REV.Status = 'Posted' AND rev.DateReceived between '2016-09-09' and 
'2016-09-09'; 

請忽略where子句中的條件,但如果你想這就是是我的其他標準在Table:Receiving

即使代碼工作似乎有什麼不對我有相同ItemCode接收有一個數據,那就是DRY0000121

QtyPack要剪切的故事,總之,這裏是我的問題

我怎樣才能在表Variance傳輸之前總結所有ItemCode?例如DRY000012在接收傳輸之前必須變爲24。

TYSM爲今後幫助

回答

0
UPDATE Variance t1 
INNER JOIN 
(
    SELECT ItemCode, 
      Status, 
      DateReceived, 
      REPLACE(QtyPack, ',', '') * REPLACE(PCS, ',', '') + 
      REPLACE(QtyStan, ',', '') AS amount 
    FROM Receiving 
) t2 
    ON t1.ItemCode = t2.ItemCode 
SET t1.Receiving = t2.amount 
WHERE t2.Status = 'Posted' AND 
     t2.DateReceived BETWEEN '2016-09-09' AND '2016-09-09' 

這假定一個ItemCode只有在Receiving表中出現一次,而我們沒有做UPDATE之前需要任何形式的聚集。

+0

TYSM的幫助,但我做了一些改變 –

+1

真的很棒的幫助,已批准 –