2011-05-19

update 使用 case when

更新資料時若需經過case when給對應的值,
下列範例可達到要求。
update HBSpecM set PURWgt=(case when PUR.QtyCH is NULL then 0 else PUR.QtyCH end)
from HBSpecM HBM left join
	(select D.BookNum,HBM.ProdNo,QtyCH=SUM(QtyCH)
	from PUR510D D left join HBMatAnti HBM on D.CorpNo=HBM.CorpNo and D.ProdNo=HBM.AntiProdNo
	where D.CorpNo='20001'
	group by D.BookNum,HBM.ProdNo) PUR on HBM.BookNum=PUR.BookNum and HBM.ProdNo=PUR.ProdNo
where HBM.CorpNo='20001'

沒有留言: