" (select * from Set_unsettle where Version='{1}') a\n"+
" LEFT JOIN (\n"+
" SELECT\n"+
" OrderBillNum,\n"+
" ChassisNumber,\n"+
" ItemCode,\n"+
" KENNCode,\n"+
" ChassisNumber2\n"+
"STUFF((SELECT ',' + ErpMaterialCode FROM Set_fis subTitle WHERE KENNCode =tm1.KENNCode\n"+
"and ChassisNumber2=tm1.ChassisNumber2 and ItemCode=tm1.ItemCode FOR XML PATH('')),1, 1, '') AS ErpMaterialCode,\n"+
"STUFF((SELECT ',' + WMSBillNum FROM Set_fis subTitle WHERE KENNCode =tm1.KENNCode\n"+
"and ChassisNumber2=tm1.ChassisNumber2 and ItemCode=tm1.ItemCode FOR XML PATH('')),1, 1, '') AS WMSBillNum,\n"+
" 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();