use kunming_yefa DROP TABLE #Source SELECT A.FBillId,A.FBillNo,A.FBillTime,B.FMemCourseId,B.FPurTimes,B.FPurAmount into #Source FROM TMemberSaleBill AS A,TMemberSaleExchangeSource AS B WHERE A.FBillId = B.FBillId AND B.FMemCourseId IN(SELECT FId FROM TMemberCourse WHERE FCompID != 3472894337749090304) AND A.FCompId= 3472894337749090304 SELECT * FROM #Source SELECT A.FBillId,A.FBillNo,A.FBillTime,B.FMemCourseId,B.FPurTimes,B.FPurAmount into #Source FROM TMemberSaleBill AS A,TMemberSaleExchangeSource AS B WHERE A.FBillId = B.FBillId AND B.FMemCourseId IN(SELECT FId FROM TMemberCourse WHERE FCompID != 3472894337749090304) AND A.FCompId= 3472894337749090304 SELECT * FROM TMemberSaleBill as a,TMemberSaleExchange AS B WHERE A.FBillId IN(SELECT FBillId FROM #Source) AND A.FBillId = B.FBillId AND FCardId IN(SELECT FCardId FROM TMemberCard WHERE FCompId = 3472894337749090304) SELECT SUM(FPurAmount) FROM #Source SELECT SUM(FPurAmount) FROM TMemberSaleCourse WHERE FBillId IN(SELECT FBillId FROM #Source) SELECT * FROM #Source WHERE FBillId IN( SELECT FBillId FROM TMemberSaleCourse WHERE FBillId IN(SELECT FBillId FROM #Source) ) 3472894337749090304 SELECT * FROM TCompany