SELECT 
	t0.FID
	,t0.FBILLNO
	,t0e.FMATERIALID
	,t0e.FMUSTQTY
	,t0e.FPICKEDQTY
	,t0e.FINVQTY
	,t0e.FCANPICKEDQTY
	,t0e.FLACKQTY
	,t0e.FBOMID
	,t0e.FSUMMARYID
	--,t0e.*
FROM T_PRD_LACKANALYSEBILL t0 
	INNER JOIN T_PRD_LACKENTRY t0e on t0e.FID = t0.FID
--where t0.FID = 101203
SELECT 
	--t0.FID
	--,t0.FBILLNO
	--,t0e.FMATERIALID
	--,t0e.FSELECTED
	--,t0e.FQTY
	t0e.*
FROM T_PRD_LACKANALYSEBILL t0 
	INNER JOIN T_PRD_LACKMOENTRY t0e on t0.FID = t0e.FID

SELECT t1.FNUMBER,t0e_a.FSUMMARYID,*
FROM T_PRD_PMPPBOMENTRY t0e
	INNER JOIN T_PRD_PMPPBOMENTRY_A t0e_a on t0e.FENTRYID = t0e_a.FENTRYID
	INNER JOIN T_BD_MATERIAL t1 on t1.FMATERIALID = t0e.FMATERIALIDSUB
where t0e.FID = 101203
	--AND t1.FNUMBER = 'TSB074H000108O'


--MERGE INTO T_PRD_LACKENTRY U1 USING(
--SELECT S.FID,S.FSUMMARYID,S.FENTRYID,S1.FMOBILLNO 
--FROM (
--	SELECT T1.FID,T2.FSUMMARYID,MIN(T1.FENTRYID) AS FENTRYID 
--	FROM T_PRD_PMPPBOMENTRY T1 
--		INNER JOIN T_PRD_PMPPBOMENTRY_A T2 ON T1.FENTRYID = T2.FENTRYID --and t1.fid = @lFId 
--	GROUP BY T1.FID,T2.FSUMMARYID) S 
--	INNER JOIN T_PRD_PMPPBOMENTRY S1 ON S.FENTRYID = S1.FENTRYID 
--	INNER JOIN T_PRD_PMPPBOMENTRY_A S2 ON S.FENTRYID = S2.FENTRYID) U2 ON U1.FID = U2.FID AND U1.FSUMMARYID = U2.FSUMMARYID 
--WHEN MATCHED 
--THEN UPDATE SET U1.F_ISVCODE_MOBILLNO = U2.FMOBILLNO;

--SELECT S.FID,S.FSUMMARYID,S.FENTRYID,S1.FMOBILLNO 
--FROM (
--	SELECT T1.FID,T2.FSUMMARYID,MIN(T1.FENTRYID) AS FENTRYID 
--	FROM T_PRD_PMPPBOMENTRY T1 
--		INNER JOIN T_PRD_PMPPBOMENTRY_A T2 ON T1.FENTRYID = T2.FENTRYID --and t1.fid = @lFId 
--	GROUP BY T1.FID,T2.FSUMMARYID) S 
--	INNER JOIN T_PRD_PMPPBOMENTRY S1 ON S.FENTRYID = S1.FENTRYID 
--	INNER JOIN T_PRD_PMPPBOMENTRY_A S2 ON S.FENTRYID = S2.FENTRYID

