 SELECT 
	t0.FBILLNO
	,t0e.FSRCBILLNO
	,t0e.FENTRYID
	,t1e.F_CYG_WORKTYPE
	,t1e.FFINISHQTY
	,t1e.FSTDMANHOUR
	,t1e.FHRWORKTIME
	,t2p.FPERUNITSTANDHOUR
	,CASE t1e.F_CYG_WORKTYPE WHEN '' THEN t1e.FHRWORKTIME ELSE (CASE t1e.FSTDMANHOUR WHEN 0 THEN t1e.FHRWORKTIME ELSE t1e.FSTDMANHOUR * t1e.FFINISHQTY END) END 'NEW_FHRWORKTIME'
	,t2.F_CYG_FITTIME
	,CASE t1e.F_CYG_WORKTYPE WHEN '1' THEN t2.F_CYG_FITTIME * t1e.FFINISHQTY ELSE 0 END 'NEW_FITTIME'
	,t2.F_CYG_GUIDETIME
	,CASE t1e.F_CYG_WORKTYPE WHEN '2' THEN t2.F_CYG_GUIDETIME * t1e.FFINISHQTY ELSE 0 END 'NEW_GUIDETIME'
	INTO #temp
 FROM T_CB_WORKHOURS t0
	INNER JOIN  T_CB_WORKHOURSENTRY t0e on t0.FID = t0e.FID
	INNER JOIN T_PRD_MORPTENTRY t1e on t1e.FENTRYID = t0e.FSRCENTRYID 
		AND t1e.FID = t0e.FSRCBILLID AND t1e.FSEQ =  t0e.FSRCSEQ AND t0e.FSRCBILLFORMID = 'PRD_MORPT'
	INNER JOIN T_BD_MATERIAL t2 on t2.FMATERIALID = t1e.FMATERIALID
	INNER JOIN T_BD_MATERIALPRODUCE t2p on t2p.FMATERIALID = t1e.FMATERIALID

--SELECT 
--	t0.FBILLNO
--	,t0.FSRCBILLNO
--	,t0.FENTRYID
--	,t0.F_CYG_WORKTYPE
--	,t0.FFINISHQTY
--	,t0.FSTDMANHOUR
--	,t0.FHRWORKTIME
--	,t0.FPERUNITSTANDHOUR
--	,t0.NEW_FHRWORKTIME
--	,t0.F_CYG_FITTIME
--	,t0.NEW_FITTIME
--	,t0.F_CYG_GUIDETIME
--	,t0.NEW_GUIDETIME
--	INTO #temp1
--FROM #temp t0
--WHERE t0.FSTDMANHOUR = 0
UPDATE t0e 
SET t0e.F_CYG_WORKTYPE = t1.F_CYG_WORKTYPE
	,t0e.FFitTimeTotal =t1.NEW_FITTIME 
	,t0e.FguideTimeTotal = t1.NEW_GUIDETIME
	,t0e.FHrWorkTimePart = t1.NEW_FHRWORKTIME
FROM T_CB_WORKHOURSENTRY t0e 
	INNER JOIN #temp t1 on t0e.FENTRYID = t1.FENTRYID

SELECT * FROM #temp
DROP TABLE #temp
--SELECT * FROM #temp1
--DROP TABLE #temp1