use qingdao_oufan SELECT * FROM TCompany SELECT * FROM TBasicType WHERE FKey like '%dep%' SELECT M.FMemNO AS 会员编号,M.FName 姓名,M.FMobilePhone 手机,dbo.FNNumToStr(SUM(A.FPayAmt)) AS '消费金额' FROM ( SELECT A.FMemId,SUM(C.FPayAmt) AS FPayAmt FROM TConsumeGoods AS B,TConsumeBill AS A,TConsumeGoodsPayment AS C WHERE B.FDeptId IN (SELECT FId FROM TBasicType WHERE FKey = 'Department' AND FName in('美疗部', '美发部')) AND A.FBillId = B.FBillId AND B.FId = C.FDetailId AND C.FPayTypeId IN(SELECT FId FROM TPayAccount WHERE FIsCashIncome = 1) AND A.FState = 1 AND A.FWorkDate BETWEEN'2019-08-01' AND '2020-07-30' AND FMemId IN(SELECT FMemId FROM TMemberCard WHERE FCompId = dbo.FNCompId('001') UNION ALL SELECT FMemId FROM TMemberCourse WHERE FCompID=dbo.FNCompId('001')) GROUP BY A.FMemId UNION ALL SELECT A.FMemId,SUM(C.FPayAmt) AS FPayAmt FROM TConsumeItem AS B,TConsumeBill AS A,TConsumeItemPayment AS C WHERE B.FDeptId IN (SELECT FId FROM TBasicType WHERE FKey = 'Department' AND FName in('美疗部' ,'美发部')) AND A.FBillId = B.FBillId AND B.FId = C.FDetailId AND C.FPayTypeId IN(SELECT FId FROM TPayAccount WHERE FIsCashIncome = 1) AND A.FState = 1 AND A.FWorkDate BETWEEN'2019-08-01' AND '2020-08-31' GROUP BY A.FMemId UNION ALL SELECT A.FMemId,SUM(C.FPayAmt) AS FPayAmt FROM TMemberSaleBill AS A,TMemberSaleDetailSplit AS C WHERE C.FDeptId IN (SELECT FId FROM TBasicType WHERE FKey = 'Department' AND FName in('美疗部' ,'美发部')) AND A.FBillId = C.FBillId AND C.FPayTypeId IN(SELECT FId FROM TPayAccount WHERE FIsCashIncome = 1) AND A.FState = 1 AND A.FWorkDate BETWEEN'2020-04-01' AND '2020-09-30' GROUP BY A.FMemId ) AS A,TMember AS M WHERE M.FMemId = A.FMemId GROUP BY A.FMemId,M.FMemNo,M.FName,M.FMobilePhone ORDER BY SUM(A.FPayAmt) DESC