" (select * from Set_unsettle where Version='{1}') a\n"+
//" temp1.MaterialCode,\n" +
" LEFT JOIN (\n"+
//" temp1.KENNCode,\n" +
" SELECT\n"+
//" temp1.ChassisNumber2,\n" +
" OrderBillNum,\n"+
//" temp1.Qty,\n" +
" ChassisNumber,\n"+
//" temp1.ChassisNumber,\n" +
" ItemCode,\n"+
//" temp1.CP5Time,\n" +
" KENNCode,\n"+
//" temp1.UnSettleQty,\n" +
" ChassisNumber2\n"+
//" temp1.SapMaterialCode,\n" +
//" temp1.MaterialDesc,\n" +
"STUFF((SELECT ',' + ErpMaterialCode FROM Set_fis subTitle WHERE KENNCode =tm1.KENNCode\n"+
//" temp1.MaterialGroup,\n" +
"and ChassisNumber2=tm1.ChassisNumber2 and ItemCode=tm1.ItemCode FOR XML PATH('')),1, 1, '') AS ErpMaterialCode,\n"+
//" temp2.MaterialCode AS ParentSapMaterialCode,\n" +
"STUFF((SELECT ',' + WMSBillNum FROM Set_fis subTitle WHERE KENNCode =tm1.KENNCode\n"+
//" temp2.MaterialDesc AS ParentMaterialDesc \n" +
"and ChassisNumber2=tm1.ChassisNumber2 and ItemCode=tm1.ItemCode FOR XML PATH('')),1, 1, '') AS WMSBillNum,\n"+
//" FROM\n" +
//" (\n" +
//" SELECT\n" +
//" a.WMSState,\n" +
//" a.WMSBillNum,\n" +
//" a.MaterialCode,\n" +
//" a.KENNCode,\n" +
//" a.ChassisNumber2,\n" +
//" a.Qty,\n" +
//" a.ParentCode,\n" +
//" a.ChassisNumber,\n" +
//" a.CP5Time,\n" +
//" b.Qty AS UnSettleQty,\n" +
//" C.MaterialCode AS SapMaterialCode,\n" +
//" C.MaterialDesc,\n" +
//" C.EstimateTypeDesc AS MaterialGroup \n" +
//" FROM\n" +
//" (\n" +
//" SELECT\n" +
//" WMSState,\n" +
//" WMSBillNum,\n" +
//" ItemCode AS MaterialCode,\n" +
//" KENNCode,\n" +
//" ChassisNumber2,\n" +
//" SUM( Qty ) AS Qty,\n" +
//" ErpMaterialCode AS ParentCode,\n" +
//" ChassisNumber,\n" +
//" CP5Time \n" +
//" FROM\n" +
//" Set_fis \n" +
//" WHERE\n" +
//" ( State = 0 ) \n" +
//" GROUP BY\n" +
//" ItemCode,\n" +
//" KENNCode,\n" +
//" ChassisNumber2,\n" +
//" WMSState,\n" +
//" WMSBillNum,\n" +
//" ErpMaterialCode,\n" +
//" ChassisNumber,\n" +
//" CP5Time \n" +
//" ) AS a\n" +
//" LEFT OUTER JOIN Set_Unsettle AS b ON a.ChassisNumber2 = b.ChassisNumber \n" +
//" AND b.MaterialCode = a.MaterialCode \n" +
//" AND a.KENNCode = b.KENNCode\n" +
//" LEFT OUTER JOIN Set_material AS C ON a.MaterialCode = C.CustomerPartCode {0}\n" +
//" ) AS temp1\n" +
//" LEFT OUTER JOIN Set_material AS temp2 ON temp1.ParentCode = temp2.MaterialCode \n" +
//" ) t1\n" +
//" LEFT JOIN (\n" +
//" SELECT\n" +
//" MaterialCode,\n" +
//" Price \n" +
//" FROM\n" +
//" Set_PriceList \n" +
//" WHERE\n" +
//" version = ( SELECT max( version ) FROM Set_PriceListVersion )) t2 ON t1.SapMaterialCode = t2.MaterialCode";
" sum( qty ) qty \n"+
" FROM\n"+
" Set_fis tm1\n"+
" WHERE\n"+
" ( state = 0 \n"+
" OR state = 4) {0} \n"+
" GROUP BY\n"+
" ItemCode,\n"+
" KENNCode,\n"+
" ChassisNumber2,\n"+
" OrderBillNum,\n"+
" ChassisNumber,\n"+
" ) b ON a.ChassisNumber = b.ChassisNumber2 \n"+
" AND a.MaterialCode = b.ItemCode \n"+
" AND a.KENNCode = b.KENNCode\n"+
" LEFT JOIN Set_material c ON a.MaterialCode = c.CustomerPartCode \n"+
" ) temp1\n"+
" LEFT JOIN (select max(Id) Id,MaterialCode,MaterialDesc from Set_material group by MaterialCode,MaterialDesc) temp2 ON SUBSTRING(temp1.ParentSapMaterialCode,1,8) = temp2.MaterialCode \n"+
" ) t1\n"+
" LEFT JOIN (\n"+
" SELECT\n"+
" MaterialCode,\n"+
" Price \n"+
" FROM\n"+
" Set_PriceList \n"+
" WHERE\n"+
" version = ( SELECT max( version ) FROM Set_PriceListVersion )) t2 ON t1.SapMaterialCode = t2.MaterialCode \n"+
"and ItemCode = tm1.MaterialCode FOR XML PATH('')),\n"+
"1,\n"+
"1,\n"+
"'') AS ParentSapMaterialCode,\n"+
"STUFF((\n"+
"SELECT\n"+
"',' + WMSBillNum\n"+
"FROM\n"+
"Set_fis subTitle\n"+
"WHERE\n"+
"KENNCode = tm1.KENNCode2\n"+
"and ChassisNumber2 = tm1.ChassisNumber2\n"+
"and ItemCode = tm1.MaterialCode FOR XML PATH('')),\n"+
"1,\n"+
"1,\n"+
"'') AS WMSBillNum,\n"+
"STUFF((\n"+
"SELECT\n"+
"',' + cast(CP5Time as varchar(20))\n"+
"FROM\n"+
"Set_fis subTitle\n"+
"WHERE\n"+
"KENNCode = tm1.KENNCode2\n"+
"and ChassisNumber2 = tm1.ChassisNumber2\n"+
"and ItemCode = tm1.MaterialCode FOR XML PATH('')),\n"+
"1,\n"+
"1,\n"+
"'') AS CP5Time,\n"+
"STUFF((\n"+
"SELECT\n"+
"',' + UnSettleVersion\n"+
"FROM\n"+
"Set_fis subTitle\n"+
"WHERE\n"+
"KENNCode = tm1.KENNCode2\n"+
"and ChassisNumber2 = tm1.ChassisNumber2\n"+
"and ItemCode = tm1.MaterialCode FOR XML PATH('')),\n"+
"1,\n"+
"1,\n"+
"'') AS UnSettleVersion,\n"+
"sum(qty) qty\n"+
"from\n"+
"\n"+
"(\n"+
"SELECT\n"+
"WMSState,\n"+
"WMSBillNum,\n"+
"UnSettleVersion,\n"+
"BeginTime CP5Time,\n"+
"ItemCode MaterialCode,\n"+
"ChassisNumber,\n"+
"OrderBillNum KENNCode,\n"+
"ChassisNumber2,\n"+
"KENNCode KENNCode2,\n"+
"QTY,\n"+
"ErpMaterialCode ,\n"+
"'' FLAG\n"+
"FROM\n"+
"Set_fis\n"+
"where\n"+
"1 = 1\n"+
"AND STATE = 0\n"+
"UNION ALL\n"+
"SELECT\n"+
"\n"+
"WMSState,\n"+
"WMSBillNum,\n"+
"UnSettleVersion,\n"+
"BeginTime CP5Time,\n"+
"ItemCode MaterialCode,\n"+
"ChassisNumber,\n"+
"OrderBillNum KENNCode,\n"+
"ChassisNumber2,\n"+
"KENNCode KENNCode2,\n"+
"QTY,\n"+
"ErpMaterialCode ,\n"+
"'L' FLAG\n"+
"FROM\n"+
"Set_fis\n"+
"WHERE\n"+
"1 = 1\n"+
"AND STATE = 4\n"+
")\n"+
"tm1\n"+
"GROUP BY\n"+
"MaterialCode,\n"+
"KENNCode2,\n"+
"ChassisNumber2,\n"+
"KENNCode,\n"+
"ChassisNumber,\n"+
"FLAG\n"+
") A\n"+
"left join (\n"+
"select\n"+
"*\n"+
"from\n"+
"Set_Unsettle\n"+
"where\n"+
"version = '{1}') f\n"+
"on\n"+
"a.MaterialCode = f.MaterialCode\n"+
"and a.KENNCode2 = f.KENNCode\n"+
"and a.ChassisNumber2 = f.ChassisNumber\n"+
"LEFT JOIN Set_material B ON\n"+
"a.MaterialCode = b.CustomerPartCode\n"+
"LEFT JOIN (\n"+
"select\n"+
"max(Id) Id,\n"+
"MaterialCode,\n"+
"MaterialDesc\n"+
"from\n"+
"Set_material\n"+
"group by\n"+
"MaterialCode,\n"+
"MaterialDesc) d ON\n"+
"a.ParentSapMaterialCode = d.MaterialCode\n"+
") temp1\n"+
"LEFT JOIN (\n"+
"SELECT\n"+
"Price,\n"+
"MaterialCode\n"+
"FROM\n"+
"Set_PriceList\n"+
"WHERE\n"+
"version = (\n"+
"SELECT\n"+
"Max(Version)\n"+
"FROM\n"+
"Set_PriceList ) ) temp2 ON\n"+
"temp1.SapMaterialCode = temp2.MaterialCode\n";
//var _materialList = DbConnection.Query<Material>("select * from set_material").ToList();
//var _priceList = DbConnection.Query<PriceList>("select MaterialCode,Price from Set_PriceList where version=(select max(version) from set_Set_PriceListVersion)").ToList();