IF object_id('dbo.PRptMemberReport','P') IS NOT NULL BEGIN DROP PROCEDURE dbo.PRptMemberReport END GO CREATE PROCEDURE PRptMemberReport ( @IParam VARCHAR(8000) --参数 ) AS BEGIN -- exec PDropTempTables --IF object_id('#Member') IS NOT NULL --BEGIN -- DROP TABLE #Member --END SELECT @IParam = dbo.FNToDBC(@IParam) CREATE TABLE #Member ( --FSId INT NOT NULL, FCompId BIGINT NOT NULL, FMemId BIGINT NOT NULL, FMemNo VARCHAR(50) NOT NULL, --会员编号 FName VARCHAR(80) NOT NULL, --会员姓名 FCompNo VARCHAR(60) NOT NULL, --归属公司 FGender INT NOT NULL, --性别 FMobilePhone VARCHAR(50) NOT NULL, --手机号码 FBirthday VARCHAR(30) NULL, -- 生日 FBirthdayType CHAR NOT NULL, --生日类型 FJoinDate VARCHAR(30) NOT NULL, --入会日期 FIsVisitor BIT,--散客 FRchgAmt DECIMAL(18,6) NOT NULL DEFAULT 0, --累计充值 FConsAmt DECIMAL(18,6) NOT NULL DEFAULT 0, --累计消费 FCardConsAmt DECIMAL(18,6) NOT NULL DEFAULT 0, --累计储值消费 FConsTimes INT NOT NULL DEFAULT 0, --消费总次数 FLastDate VARCHAR(20) NULL, --最后消费日期 FPoint DECIMAL(18,6) NOT NULL DEFAULT 0, --积分 FArrear DECIMAL(18,6) NOT NULL DEFAULT 0, --欠款 FConsArrear DECIMAL(18,6) NOT NULL DEFAULT 0,--消费欠款 FConsPrice DECIMAL(18,6) NOT NULL DEFAULT 0, FCourseArrear DECIMAL(18,6) NOT NULL DEFAULT 0, FFavoriteEmpName VARCHAR(100), --喜爱的发型师 FSaleEmpName VARCHAR(100), --销售者 FChargeEmpId BIGINT, --负责员工 FMemMemo VARCHAR(1000), --会员备注 PRIMARY KEY(FMemId) ) --=IF object_id('dbo.#MemberCard') IS NOT NULL --BEGIN -- DROP TABLE #MemberCard --END CREATE TABLE #MemberCard ( -- FSId INT NOT NULL, --登录id FCardKind VARCHAR(50) , FCardId BIGINT NOT NULL, --卡Id FCardNo VARCHAR(50) NOT NULL, --卡号 FMemId BIGINT NOT NULL, --会员Id FMemNo VARCHAR(50) NOT NULL, --会员编号 FCompId BIGINT NOT NULL, --公司Id FCardTypeId BIGINT NOT NULL, FCardTypeNo VARCHAR(1000) NOT NULL, --类别 FCardTypeName VARCHAR(100) NOT NULL, --类别名称 FCreateDate VARCHAR(20) NOT NULL, --买卡日期 FExpiredDate VARCHAR(20) NULL, --有效期 FPurAmount DECIMAL(18,6), --购买金额 FRemainTimes INT, --剩余次数 FRemainPurAmount DECIMAL(18,6) , --剩余金额 FRemainFreeAmount DECIMAL(18,6) , --剩余金额 FState INT, --状态 FCalcMode INT DEFAULT 1, FItemId BIGINT DEFAULT 0, FCardMemo VARCHAR(1000), --卡备注 FRchgAmt DECIMAL(18,6) NOT NULL DEFAULT 0, --累计充值 FConsAmt DECIMAL(18,6) NOT NULL DEFAULT 0, --累计消费 FCardConsAmt DECIMAL(18,6) NOT NULL DEFAULT 0, --累计储值消费 FConsTimes INT NOT NULL DEFAULT 0, --消费总次数 FLastDate VARCHAR(20) NULL, --最后消费日期 FBizEmpId BIGINT, --负责业务 FBizEmpName VARCHAR(100), --负责业务 PRIMARY KEY(FCardId,FCardKind) ) CREATE TABLE #MemberCardAccount ( FMemId BIGINT, FCardId BIGINT, FAcctId BIGINT, FBalance DECIMAL(18,6), FArrear DECIMAL(18,6) ) DECLARE @SId INT DECLARE @CompanyForCard BIT --使用卡归属公司 DECLARE @MemCompNo VARCHAR(1000) --会员公司 DECLARE @CompSource INT --公司 DECLARE @ShowCardInfo BIT --显示卡 DECLARE @ShowCourseInfo INT --显示疗程 -- DECLARE @CardCompNo VARCHAR(1000)--储值卡公司 DECLARE @CompanyStr VARCHAR(1000)--公司 DECLARE @FromMemNo VARCHAR(30) --开始会员编号 DECLARE @ToMemNo VARCHAR(30) --结束会员编号 DECLARE @MemName VARCHAR(50) --会员姓名 DECLARE @MobilePhone VARCHAR(20) --手机号码 DECLARE @FromBirthday VARCHAR(20)--开始生日 1-0121 DECLARE @ToBirthday VARCHAR(20) --结束生日 1-0212 DECLARE @FromJoinDate DATETIME --开始加入日期 DECLARE @ToJoinDate DATETIME --截至加入日期 DECLARE @Gender INT --性别 DECLARE @FromPoint DECIMAL(18,6) --开始积分 DECLARE @ToPoint DECIMAL(18,6) --结束积分 DECLARE @FromArrear DECIMAL(18,6) --开始欠款 DECLARE @ToArrear DECIMAL(18,6) --结束欠款 DECLARE @FromLastDate VARCHAR(20)--最后消费日期 DECLARE @ToLastDate VARCHAR(20) -- DECLARE @AfterLastDate VARCHAR(20) -- DECLARE @FromConsTimes INT --消费总次数 DECLARE @ToConsTimes INT DECLARE @FromConsAmt DECIMAL(18,6)--消费总额 DECLARE @ToConsAmt DECIMAL(18,6) -- DECLARE @FromRchgAmt DECIMAL(18,6) DECLARE @ToRchgAmt DECIMAL(18,6) DECLARE @NotComeDay INT --多少天没有来的 DECLARE @FromStatDate VARCHAR(20) --消费统计日期 DECLARE @ToStatDate VARCHAR(20) --消费统计日期 DECLARE @ShowCardConsAmt BIT DECLARE @FromCardNo VARCHAR(30) --开始会员卡号 DECLARE @ToCardNo VARCHAR(30) --结束会员卡号 DECLARE @CardTypeNo VARCHAR(1000) DECLARE @FromCardCreateDate DATETIME --开卡日期 DECLARE @ToCardCreateDate DATETIME DECLARE @FromCardExpiredDate DATETIME --截至日期 DECLARE @ToCardExpiredDate DATETIME --截至账户日期 DECLARE @FromAcctExpiredDate DATETIME --截至账户日期 DECLARE @ToAcctExpiredDate DATETIME --截至日期 DECLARE @FromRemainTimes DECIMAL(18,6) --剩余次数 DECLARE @ToRemainTimes DECIMAL(18,6) --剩余次数 DECLARE @CardState INT --卡状态 DECLARE @AcctNo VARCHAR(30) --账户 DECLARE @FromBalance DECIMAL(18,6) --余额 DECLARE @ToBalance DECIMAL(18,6) --余额 DECLARE @CardExpired INT --储值卡是否过期 -1过期,0全部,1未过期 DECLARE @BalanceCutDate VARCHAR(20) --余额截点日期 DECLARE @BizEmpNo VARCHAR(1000) --负责业务 DECLARE @FavoriteEmpNo VARCHAR(1000) --喜爱的员工 DECLARE @FavoriteEmpId BIGINT --喜爱的员工 DECLARE @BizEmpId BIGINT DECLARE @ShowAllMember BIT DECLARE @IsVisitor BIT --疗程条件 DECLARE @CourseNo VARCHAR(1000) DECLARE @FromCourseRemainTimes DECIMAL(18,6) DECLARE @ToCourseRemainTimes DECIMAL(18,6) DECLARE @FromCardPurAmount DECIMAL(18,6) --开始销售金额 DECLARE @ToCardPurAmount DECIMAL(18,6) --结束销售金额 SELECT @MemCompNo = dbo.FNGetParamValue(@IParam,'CompNo'); SELECT @CompSource = dbo.FNGetParamValue(@IParam,'CompSource'); --SELECT @CardCompNo = dbo.FNGetParamValue(@IParam,'CardCompNo'); --SELECT @SId = dbo.FNGetParamValue(@IParam,'SId'); SELECT @ShowCardInfo = ISNULL(dbo.FNGetParamValue(@IParam,'ShowCardInfo'),1); SELECT @ShowCourseInfo = ISNULL(dbo.FNGetParamValue(@IParam,'ShowCourseInfo'),0); SELECT @FromMemNo = ISNULL(dbo.FNGetParamValue(@IParam,'FromMemNo'),''); SELECT @ToMemNo = ISNULL(dbo.FNGetParamValue(@IParam,'ToMemNo'),''); SELECT @MemName = ISNULL(dbo.FNGetParamValue(@IParam,'MemName'),''); SELECT @MobilePhone = ISNULL(dbo.FNGetParamValue(@IParam,'MobilePhone'),''); SELECT @Gender = ISNULL(dbo.FNGetParamValue(@IParam,'Gender'),-1); SELECT @FromBirthday = ISNULL(dbo.FNGetParamValue(@IParam,'FromBirthday'),''); SELECT @ToBirthday = ISNULL(dbo.FNGetParamValue(@IParam,'ToBirthday'),''); SELECT @FromJoinDate = dbo.FNGetParamValue(@IParam,'FromJoinDate'); SELECT @ToJoinDate = dbo.FNGetParamValue(@IParam,'ToJoinDate'); SELECT @FromPoint = ISNULL( dbo.FNGetParamValue(@IParam,'FromPoint'),0); SELECT @ToPoint = ISNULL(dbo.FNGetParamValue(@IParam,'ToPoint'),0); SELECT @FromArrear = ISNULL( dbo.FNGetParamValue(@IParam,'FromArrear'),0); SELECT @ToArrear = ISNULL(dbo.FNGetParamValue(@IParam,'ToArrear'),0); SELECT @FromLastDate = ISNULL( dbo.FNGetParamValue(@IParam,'FromLastDate'),''); SELECT @ToLastDate = ISNULL(dbo.FNGetParamValue(@IParam,'ToLastDate'),''); SELECT @AfterLastDate = ISNULL(dbo.FNGetParamValue(@IParam,'AfterLastDate'),''); SELECT @FromConsTimes = ISNULL( dbo.FNGetParamValue(@IParam,'FromConsTimes'),NULL); SELECT @ToConsTimes = ISNULL(dbo.FNGetParamValue(@IParam,'ToConsTimes'),NULL); SELECT @FromConsAmt = ISNULL( dbo.FNGetParamValue(@IParam,'FromConsAmt'),NULL); SELECT @ToConsAmt = ISNULL(dbo.FNGetParamValue(@IParam,'ToConsAmt'),NULL); SELECT @FromRchgAmt = ISNULL( dbo.FNGetParamValue(@IParam,'FromRchgAmt'),NULL); SELECT @ToRchgAmt = ISNULL(dbo.FNGetParamValue(@IParam,'ToRchgAmt'),NULL); SELECT @NotComeDay = ISNULL(dbo.FNGetParamValue(@IParam,'NotComeDay'),0); SELECT @ShowCardConsAmt = ISNULL(dbo.FNGetParamValue(@IParam,'ShowCardConsAmt'),0) SELECT @FromStatDate = ISNULL(dbo.FNGetParamValue(@IParam,'FromStatDate'),''); SELECT @ToStatDate = ISNULL(dbo.FNGetParamValue(@IParam,'ToStatDate'),''); SELECT @FromCardNo = ISNULL(dbo.FNGetParamValue(@IParam,'FromCardNo'),''); SELECT @ToCardNo = ISNULL(dbo.FNGetParamValue(@IParam,'ToCardNo'),''); SELECT @CardState = ISNULL(dbo.FNGetParamValue(@IParam,'CardState'),0); SELECT @CardTypeNo = ISNULL(dbo.FNGetParamValue(@IParam,'CardTypeNo'),''); SELECT @FromCardCreateDate = ISNULL(dbo.FNGetParamValue(@IParam,'FromCardCreateDate'),NULL); SELECT @ToCardCreateDate = ISNULL(dbo.FNGetParamValue(@IParam,'ToCardCreateDate'),NULL); SELECT @FromCardExpiredDate = ISNULL(dbo.FNGetParamValue(@IParam,'FromCardExpiredDate'),NULL); SELECT @ToCardExpiredDate = ISNULL(dbo.FNGetParamValue(@IParam,'ToCardExpiredDate'),NULL); SELECT @FromAcctExpiredDate = ISNULL(dbo.FNGetParamValue(@IParam,'FromAcctExpiredDate'),NULL); SELECT @ToAcctExpiredDate = ISNULL(dbo.FNGetParamValue(@IParam,'ToAcctExpiredDate'),NULL); SELECT @FromRemainTimes = ISNULL(dbo.FNGetParamValue(@IParam,'FromCourseRemainTimes'),0); SELECT @ToRemainTimes = ISNULL(dbo.FNGetParamValue(@IParam,'ToCourseRemainTimes'),0); SELECT @FromBalance = ISNULL(dbo.FNGetParamValue(@IParam,'FromBalance'),0); SELECT @ToBalance = ISNULL(dbo.FNGetParamValue(@IParam,'ToBalance'),0); SELECT @AcctNo = ISNULL(dbo.FNGetParamValue(@IParam,'AcctNo'),''); SELECT @CardExpired = ISNULL(dbo.FNGetParamValue(@IParam,'CardExpired'),0); SELECT @BalanceCutDate = ISNULL(dbo.FNGetParamValue(@IParam,'BalanceCutDate'),''); SELECT @BizEmpNo = ISNULL(ISNULL(dbo.FNGetParamValue(@IParam,'BizEmpNo'), dbo.FNGetParamValue(@IParam,'ChargeEmpNo')),''); SELECT @FavoriteEmpNo = ISNULL(dbo.FNGetParamValue(@IParam,'FavoriteEmpNo'),''); SELECT @ShowAllMember = ISNULL(dbo.FNGetParamValue(@IParam,'ShowAllMember'),1); SELECT @IsVisitor = ISNULL(dbo.FNGetParamValue(@IParam,'IsVisitor'),0); SELECT @FromCourseRemainTimes = ISNULL(dbo.FNGetParamValue(@IParam,'FromCourseRemainTimes'),0); SELECT @ToCourseRemainTimes = ISNULL(dbo.FNGetParamValue(@IParam,'ToCourseRemainTimes'),0); SELECT @CourseNo = ISNULL(dbo.FNGetParamValue(@IParam,'CourseNo'),''); SELECT @FromCardPurAmount = ISNULL(dbo.FNGetParamValue(@IParam,'FromCardPurAmount'),'0'); SELECT @ToCardPurAmount = ISNULL(dbo.FNGetParamValue(@IParam,'ToCardPurAmount'),'0'); CREATE TABLE #MemCompIds ( FCompId BIGINT ) /* DECLARE @CardCompIds TABLE ( FCompId BIGINT ) */ CREATE TABLE #CardTypeIds ( FCardTypeId BIGINT ) CREATE TABLE #CourseIds ( FCourseId BIGINT ) IF @MemCompNo = ''-- AND @CardCompNo = '' BEGIN RETURN END --会员公司 IF @MemCompNo = 'All' OR @MemCompNo = '' BEGIN INSERT #MemCompIds SELECT FId FROM TCompany END ELSE BEGIN INSERT #MemCompIds SELECT A.FId FROM TCompany AS A WITH(NOLOCK), dbo.FNStrSplit(@MemCompNo) AS B WHERE A.FNo = B.Field END --储值卡类别 IF @CardTypeNo != '' BEGIN INSERT #CardTypeIds SELECT FId FROM TMemberCardType WITH(NOLOCK) WHERE FNo IN (SELECT Field FROM dbo.FNStrSplit( @CardTypeNo)) END IF @CourseNo != '' BEGIN INSERT #CourseIds SELECT FId FROM TCourse WITH(NOLOCK) WHERE FNo IN(SELECT Field FROM dbo.FNStrSplit(@CourseNo)) END IF @FromCardCreateDate IS NOT NULL BEGIN SELECT @FromCardCreateDate = @FromCardCreateDate + ' 00:00:00' SELECT @ToCardCreateDate = @ToCardCreateDate + ' 23:59:59' END IF @FromCardExpiredDate IS NOT NULL BEGIN SELECT @FromCardExpiredDate = @FromCardExpiredDate + ' 00:00:00' SELECT @ToCardExpiredDate = @ToCardExpiredDate + ' 23:59:59' END IF @FromAcctExpiredDate IS NOT NULL BEGIN SELECT @FromAcctExpiredDate = @FromAcctExpiredDate + ' 00:00:00' SELECT @ToAcctExpiredDate = @ToAcctExpiredDate + ' 23:59:59' END IF ISNULL(@ToStatDate,'') != '' BEGIN SELECT @FromStatDate = @FromStatDate + ' 00:00:00' SELECT @ToStatDate = @ToStatDate + ' 23:59:59' END IF ISNULL(@ToJoinDate,'') != '' BEGIN SELECT @FromJoinDate = @FromJoinDate + ' 00:00:00' SELECT @ToJoinDate = @ToJoinDate + ' 23:59:59' END --今天日期 DECLARE @Today DATETIME SELECT @Today = CONVERT(varchar(100), GETDATE(), 23) SELECT @BizEmpId = 0 IF @BizEmpNo != '' BEGIN SELECT @BizEmpId = FId FROM TEmployee WHERE FNo = @BizEmpNo END SELECT @FavoriteEmpId = 0 IF @FavoriteEmpNo != '' BEGIN SELECT @FavoriteEmpId = FId FROM TEmployee WHERE FNo = @FavoriteEmpNo END SELECT @FromBirthday = REPLACE(@FromBirthday,'1-','G-'), @ToBirthday = REPLACE(@ToBirthday,'1-','G-'), @FromBirthday = REPLACE(@FromBirthday,'2-','L-'), @ToBirthday = REPLACE(@ToBirthday,'2-','L-') --初始化会员表 DECLARE @InsertMember VARCHAR(8000) DECLARE @InsertCard VARCHAR(8000) DECLARE @InsertCourse VARCHAR(8000) DECLARE @MemberWhere VARCHAR(8000) DECLARE @CardWhere VARCHAR(8000) DECLARE @CourseWhere VARCHAR(8000) DECLARE @CardParam NVARCHAR(4000) DECLARE @MemParam NVARCHAR(4000) DECLARE @CourseParam NVARCHAR(4000) SELECT @InsertMember = 'INSERT #Member(FCompId,FCompNo, FMemId,FMemNo,FName,FGender,FMobilePhone,FJoinDate,FIsVisitor,FBirthday,FBirthdayType,FPoint,FChargeEmpId,FSaleEmpName,FMemMemo,FLastDate,FConsTimes,FConsAmt,FRchgAmt)' + ' SELECT A.FCompId,'''' AS FCompNo,FMemId AS FMemId, FMemNo AS FMemNo,A.FName AS FName,A.FGender,A.FMobilePhone,CONVERT(varchar(10),A.FCreateDate,23) AS FJoinDate,A.FIsVisitor, ' + ' ISNULL(CONVERT(varchar(10), A.FBirthday, 23),'''') AS FBirthday,A.FBirthdayType,FPoint,FChargeEmpId,'''' AS FSaleEmpName,A.FMemo,' + ' CAST('''' AS VARCHAR(10)) AS FLastDate,' + ' CAST(0 AS INT) AS FConsTimes,' + ' CAST(0 AS DECIMAL(18,6)) AS FConsAmt,' + ' CAST(0 AS DECIMAL(18,6)) AS FRchgAmt' + ' FROM TMember AS A WITH(NOLOCK)' SELECT @MemberWhere = ' WHERE 1=1' + ' AND A.FMemId > 99' + ' AND A.FDeleted = 0' + ' AND A.FState = 1 '--转卡' + ' AND (@FromMemNo = '''' OR A.FMemNo BETWEEN @FromMemNo AND @ToMemNo)' + ' AND (@MemName = '''' OR A.FName LIKE ''%'' + @MemName + ''%'')' + ' AND (@MobilePhone = '''' OR A.FMobilePhone = @MobilePhone)' + ' AND (@Gender = -1 OR A.FGender = @Gender)' + ' AND (@ToPoint = 0 OR A.FPoint BETWEEN @FromPoint + 0.001 AND @ToPoint)' + ' AND (@FromBirthday = '''' OR A.FShortBirthday BETWEEN @FromBirthday AND @ToBirthday)' + ' AND (@FromJoinDate IS NULL OR A.FCreateDate BETWEEN @FromJoinDate AND @ToJoinDate)' + ' AND (@IsVisitor IS NULL OR A.FIsVisitor = @IsVisitor)' -- + ' AND (@BizEmpId = 0 OR A.FChargeEmpId = @BizEmpId)' SELECT @InsertCard = 'INSERT #MemberCard(FCompId,FCardKind,FCardId,FCardNo,FMemId,FMemNo,FCardTypeId,FCardTypeNo,FCardTypeName,FState,FPurAmount,FRemainTimes,FCardMemo,FCreateDate,FExpiredDate,FBizEmpId)' + ' SELECT A.FCompId,''Money'',A.FCardId AS FCardId,A.FCardNo AS FCardNo,A.FMemId,'''' AS FMemNo,A.FTypeID,'''' AS FCardTypeNo,'''' AS FCardTypeName,A.FState,0 AS FPurAmount, A.FRemainTimes,A.FMemo AS FCardMemo,' + ' CONVERT(varchar(10), A.FCreateDate,23) AS FCreateDate,' + ' CONVERT(varchar(10), A.FExpiredDate,23) AS FExpiredDate, ' + ' A.FBizEmpId' + ' FROM TMemberCard AS A WITH(NOLOCK)' SELECT @InsertCourse = 'INSERT #MemberCard(FCompId,FCardKind,FCardId,FCardNo,FMemId,FMemNo,FCardTypeId,FCardTypeNo,FCardTypeName,FState,FPurAmount,FRemainTimes,FRemainPurAmount,FRemainFreeAmount,FCardMemo,FCreateDate,FExpiredDate,FItemId,FCalcMode,FBizEmpid)' + ' SELECT A.FCompId,''Course'',A.FId AS FCardId,'''' AS FCardNo,A.FMemId,'''' AS FMemNo,FCourseId,'''' AS FCardTypeNo,'''' AS FCardTypeName,A.FState,A.FPurAmount, A.FRemainPurTimes + A.FRemainFreeTimes,A.FRemainPurAmount,A.FRemainFreeAmount, A.FMemo ,' + ' CONVERT(varchar(10), A.FPurchaseDate,23) AS FCreateDate,' + ' CONVERT(varchar(10), A.FExpiredDate,23) AS FExpiredDate, ' + ' A.FItemId,' + ' A.FCalcMode,A.FBizEmpId' + ' FROM TMemberCourse AS A WITH(NOLOCK)' SELECT @CardWhere = 'WHERE 1=1 AND (@CardTypeNo = '''' OR A.FTypeId IN(SELECT FCardTypeId FROM #CardTypeIds)) AND (@FromCardNo = '''' OR A.FCardNo BETWEEN @FromCardNo AND @ToCardNo) AND (@CardState = 0 OR A.FState = @CardState) AND (@FromCardCreateDate IS NULL OR A.FCreateDate BETWEEN @FromCardCreateDate AND @ToCardCreateDate) AND (@FromCardExpiredDate IS NULL OR A.FExpiredDate BETWEEN @FromCardExpiredDate AND @ToCardExpiredDate) AND (@CardState = 0 OR A.FState = @CardState) AND (@CardExpired = 0 OR (@CardExpired = 1 AND A.FExpiredDate > @Today /*未过期*/) OR (@CardExpired = -1 AND A.FExpiredDate <= @Today /*过期*/)) AND A.FDeleted = 0 /*没有删除*/ AND (@BizEmpId = 0 OR A.FBizEmpId = @BizEmpId)' SELECT @CourseWhere = 'WHERE 1=1 AND (@CourseNo = '''' OR A.FCourseId IN(SELECT FCourseId FROM #CourseIds)) AND (@CardState = 0 OR A.FState = @CardState) AND (@FromCardCreateDate IS NULL OR A.FPurchaseDate BETWEEN @FromCardCreateDate AND @ToCardCreateDate) AND (@FromRemainTimes = 0 OR ((A.FRemainPurTimes + A.FRemainFreeTimes) >= @FromRemainTimes AND (A.FRemainPurTimes + A.FRemainFreeTimes) <= @ToRemainTimes)) AND (@FromCardExpiredDate IS NULL OR A.FExpiredDate BETWEEN @FromCardExpiredDate AND @ToCardExpiredDate) AND (@CardExpired = 0 OR (@CardExpired = 1 AND (A.FExpiredDate IS NULL OR A.FExpiredDate > @Today) /*未过期*/) OR (@CardExpired = -1 AND A.FExpiredDate <= @Today /*过期*/)) AND (@ToCardPurAmount = 0 OR (A.FPurAmount >= @FromCardPurAmount AND A.FPurAmount <= @ToCardPurAmount)) AND (@BizEmpId = 0 OR A.FBizEmpId = @BizEmpId)' SELECT @MemParam = N'@MemName VARCHAR(30) ,' + ' @MobilePhone VARCHAR(30),' + ' @FromMemNo VARCHAR(30),' + ' @ToMemNo VARCHAR(30),' + ' @FromPoint INT,' + ' @ToPoint INT,' + ' @FromJoinDate DATETIME,' + ' @ToJoinDate DATETIME,' + ' @Gender INT, ' + ' @IsVisitor INT, ' + ' @BizEmpId BIGINT, ' + ' @FromBirthday VARCHAR(10),' + ' @ToBirthday VARCHAR(10)' SELECT @CardParam = N'@CardTypeNo VARCHAR(30) ,' + ' @FromCardNo VARCHAR(30),' + ' @ToCardNo VARCHAR(30),' + ' @CardState INT,' + ' @FromCardCreateDate DATETIME,' + ' @ToCardCreateDate DATETIME,' + ' @FromCardExpiredDate DATETIME,' + ' @ToCardExpiredDate DATETIME,' + ' @BizEmpId bigint,' + ' @CardExpired INT, ' + ' @FromCardPurAmount DECIMAL(18,6), ' + ' @ToCardPurAmount DECIMAL(18,6), ' + ' @Today VARCHAR(10) ' SELECT @CourseParam = N'@CourseNo VARCHAR(30) ,' + ' @CardState INT,' + ' @FromCardCreateDate DATETIME,' + ' @ToCardCreateDate DATETIME,' + ' @FromCardExpiredDate DATETIME,' + ' @ToCardExpiredDate DATETIME,' + ' @CardExpired INT, ' + ' @FromCardPurAmount DECIMAL(18,6), ' + ' @ToCardPurAmount DECIMAL(18,6), ' + ' @FromRemainTimes INT, ' + ' @ToRemainTimes INT, ' + ' @BizEmpId bigint,' + ' @Today VARCHAR(10) ' DECLARE @Sql NVARCHAR(4000) IF @CompSource = 2 --以卡的归属公司为准, 先统计卡 BEGIN IF @ShowCardInfo = 1 BEGIN SELECT @Sql = @InsertCard + ' INNER JOIN #MemCompIds AS C ON C.FCompId = A.FCompId ' + @CardWhere -- PRINT @Sql exec sp_executesql @sql,@CardParam, @CardTypeNo = @CardTypeNo, @FromCardNo = @FromCardNo, @ToCardNo = @ToCardNo, @CardState = @CardState, @ToCardCreateDate = @ToCardCreateDate, @FromCardExpiredDate = @FromCardExpiredDate, @ToCardExpiredDate = @ToCardExpiredDate, @FromCardCreateDate = @FromCardCreateDate, @FromCardPurAmount = @FromCardPurAmount, @ToCardPurAmount = @ToCardPurAmount, @CardExpired = @CardExpired, @Today = @Today, @BizEmpId = @BizEmpId END IF @ShowCourseInfo = 1 BEGIN SELECT @Sql = @InsertCourse + ' INNER JOIN #MemCompIds AS C ON C.FCompId = A.FCompId ' + @CourseWhere PRINT @Sql exec sp_executesql @sql,@CourseParam, @CourseNo = @CourseNo, @CardState = @CardState, @ToCardCreateDate = @ToCardCreateDate, @FromRemainTimes = @FromRemainTimes, @ToRemainTimes = @ToRemainTimes, @FromCardCreateDate = @FromCardCreateDate, @FromCardExpiredDate = @FromCardExpiredDate, @ToCardExpiredDate = @ToCardExpiredDate, @CardExpired = @CardExpired, @FromCardPurAmount = @FromCardPurAmount, @ToCardPurAmount = @ToCardPurAmount, @Today = @Today, @BizEmpId = @BizEmpId END --EXEC (@Sql) SELECT @Sql = @InsertMember + @MemberWhere + ' AND (EXISTS(SELECT 1 FROM TMemberCard AS B,#MemCompIds AS C WHERE B.FMemId = A.FMemId AND B.FDeleted=0 AND B.FCompId = C.FCompId) OR EXISTS(SELECT 1 FROM TMemberCourse AS B,#MemCompIds AS C WHERE B.FMemId = A.FMemId AND B.FCompId = C.FCompId) OR (FIsVisitor = 1 AND EXISTS(SELECT 1 FROM #MemCompIds AS D WHERE D.FCompId = A.FCompId)) )' PRINT @Sql print @MemParam exec sp_executesql @sql,@MemParam, @MemName = @MemName, @FromMemNo = @FromMemNo, @ToMemNo = @ToMemNo, @Gender = @Gender, @IsVisitor = @IsVisitor, @FromJoinDate = @FromJoinDate, @ToJoinDate = @ToJoinDate, @FromPoint = @FromPoint, @ToPoint = @ToPoint, @BizEmpId = @BizEmpId, @MobilePhone = @MobilePhone, @FromBirthday = @FromBirthday, @ToBirthday = @ToBirthday END ELSE BEGIN SELECT @Sql = @InsertMember + ' INNER JOIN #MemCompIds AS C ON C.FCompId = A.FCompId ' + @MemberWhere PRINT @Sql print @MemParam exec sp_executesql @sql,@MemParam, @MemName = @MemName, @FromMemNo = @FromMemNo, @ToMemNo = @ToMemNo, @Gender = @Gender, @IsVisitor = @IsVisitor, @FromJoinDate = @FromJoinDate, @ToJoinDate = @ToJoinDate, @FromPoint = @FromPoint, @ToPoint = @ToPoint, @BizEmpId = @BizEmpId, @MobilePhone = @MobilePhone, @FromBirthday = @FromBirthday, @ToBirthday = @ToBirthday IF @ShowCardInfo = 1 BEGIN IF @ShowCourseInfo = 0 BEGIN SELECT @Sql = @InsertCard + @CardWhere + ' AND EXISTS(SELECT 1 FROM #MemCompIds AS C WHERE C.FCompId = A.FCompId) ' END ELSE BEGIN SELECT @Sql = @InsertCard + @CardWhere + ' AND EXISTS(SELECT 1 FROM #Member AS C WHERE C.FMemId = A.FMemId) ' END PRINT @Sql exec sp_executesql @sql,@CardParam, @CardTypeNo = @CardTypeNo, @FromCardNo = @FromCardNo, @ToCardNo = @ToCardNo, @CardState = @CardState, @ToCardCreateDate = @ToCardCreateDate, @FromCardCreateDate = @FromCardCreateDate, @FromCardExpiredDate = @FromCardExpiredDate, @ToCardExpiredDate = @ToCardExpiredDate, @CardExpired = @CardExpired, @FromCardPurAmount = @FromCardPurAmount, @ToCardPurAmount = @ToCardPurAmount, @Today = @Today, @BizEmpId = @BizEmpId END IF @ShowCourseInfo = 1 BEGIN --不符合归属公司 IF @ShowCourseInfo = 0 BEGIN SELECT @Sql = @InsertCourse + @CourseWhere + ' AND EXISTS(SELECT 1 FROM #MemCompIds AS C WHERE C.FCompId = A.FCompId) ' END ELSE BEGIN SELECT @Sql = @InsertCourse + @CourseWhere + ' AND EXISTS(SELECT 1 FROM #Member AS C WHERE C.FMemId = A.FMemId) ' END PRINT @Sql exec sp_executesql @sql,@CourseParam, @CourseNo = @CourseNo, @CardState = @CardState, @ToCardCreateDate = @ToCardCreateDate, @FromRemainTimes = @FromRemainTimes, @ToRemainTimes = @ToRemainTimes, @FromCardCreateDate = @FromCardCreateDate, @FromCardExpiredDate = @FromCardExpiredDate, @ToCardExpiredDate = @ToCardExpiredDate, @CardExpired = @CardExpired, @FromCardPurAmount = @FromCardPurAmount, @ToCardPurAmount = @ToCardPurAmount, @Today = @Today, @BizEmpId = @BizEmpId END END --交叉删除 --删除卡在会员表中不存在的 DELETE A FROM #MemberCard AS A WHERE NOT EXISTS(SELECT 1 FROM #Member AS B WHERE A.FMemId = B.FMemId) IF @ShowAllMember != 1 BEGIN DELETE A FROM #Member AS A WHERE NOT EXISTS(SELECT 1 FROM #MemberCard AS B WHERE A.FMemId = B.FMemId) END IF @ShowCourseInfo = 1 BEGIN INSERT #MemberCardAccount(FMemId,FCardId,FAcctId,FBalance,FArrear) SELECT A.FMemId,A.FCardId,6,A.FRemainPurAmount + A.FRemainFreeAmount,0 -- INTO #MemberCardAccount FROM #MemberCard AS A WHERE A.FCardKind = 'Course' END --散客 UPDATE #Member SET FName = FName + '[散客]' WHERE FIsVisitor = 1 --统计欠款 UPDATE A SET A.FArrear = B.FArrear FROM #Member AS A,( SELECT A.FMemId ,SUM(FRemainArrear) AS FArrear FROM TMemberArrear AS A,#Member AS M WHERE M.FMemId = A.FMemId GROUP BY A.FMemId) AS B WHERE A.FMemID = B.FMemId IF @FromArrear > 0 BEGIN DELETE #Member WHERE FArrear < @FromArrear OR FArrear > @ToArrear DELETE A FROM #MemberCard AS A WHERE NOT EXISTS(SELECT 1 FROM #Member AS B WHERE A.FMemId = B.FMemId) END IF @ShowCardInfo = 1 --AND @BalanceCutDate != '' BEGIN INSERT #MemberCardAccount(FMemId,FCardId,FAcctId,FBalance,FArrear) SELECT A.FMemId,A.FCardId,A.FAcctId,A.FBalance,A.FArrear -- INTO #MemberCardAccount FROM TMemberCardAccount AS A,#MemberCard AS B WHERE A.FCardId = B.FCardId AND B.FCardKind = 'Money' AND ( @FromAcctExpiredDate IS NULL OR (A.FExpiredDate IS NOT NULL AND A.FExpiredDate BETWEEN @FromAcctExpiredDate AND @ToAcctExpiredDate)) IF @FromAcctExpiredDate IS NOT NULL BEGIN DELETE A FROM #MemberCard AS A WHERE NOT EXISTS(SELECT 1 FROM #MemberCardAccount AS B WHERE A.FCardId = B.FCardId) DELETE A FROM #Member AS A WHERE NOT EXISTS(SELECT 1 FROM #MemberCard AS B WHERE A.FMemId = B.FMemId) END IF @BalanceCutDate != '' BEGIN --从账户历史获取余额 UPDATE A SET A.FBalance = 0 FROM #MemberCardAccount AS A,#MemberCard AS B WHERE A.FCardId = B.FCardId AND B.FCardKind = 'Money' -- AND B.FCreateDate > @BalanceCutDate + ' 23:59:59' UPDATE A SET A.FBalance = B.FBalance FROM #MemberCardAccount AS A,TMemberCardAcctHist AS B, ( SELECT FCardId,FAcctId,MAX(FTime) AS FTime FROM TMemberCardAcctHist AS H WHERE FTime <= @BalanceCutDate + ' 23:59:59' GROUP BY FCardId,FAcctId ) AS C WHERE A.FCardId = B.FCardId AND A.FAcctId = B.FAcctId AND B.FCardId = C.FCardId AND B.FAcctId = C.FAcctId AND B.FTime = C.FTime END --统计余额 IF @AcctNo != '' --AND @FromBalance != 0 AND @ToBalance != 0 BEGIN DECLARE @AcctId BIGINT SELECT @AcctId = FId FROM TPayAccount WITH(NOLOCK) WHERE FNo = @AcctNo IF @AcctId != 0 BEGIN DELETE A FROM #MemberCard AS A,#MemberCardAccount AS B WHERE A.FCardId = B.FCardId AND B.FAcctId = @AcctId AND B.FBalance NOT BETWEEN @FromBalance AND @ToBalance DELETE A FROM #MemberCard AS A WHERE NOT EXISTS(SELECT TOP 1 1 FROM #MemberCardAccount AS B WITH(NOLOCK) WHERE A.FCardId = B.FCardId AND B.FAcctId = @AcctId) END END END --如果有卡的条件,就把会员表根据卡表过滤 IF @CompanyForCard = 1 OR @CardTypeNo != '' OR @FromCardNo != '' OR @CardState != 0 OR @FromCardCreateDate != '' OR @FromCardExpiredDate != '' OR @AcctNo != '' BEGIN DELETE A FROM #Member AS A WHERE NOT EXISTS(SELECT 1 FROM #MemberCard AS B WHERE A.FMemId = B.FMemId) AND FIsVisitor = 0 END --select @CardTypeNo,@FromCardNo,@ToCardNo,@CardState,@FromCardCreateDate,@FromCardExpiredDate --最后消费日期 UPDATE A SET A.FLastDate = CONVERT(varchar(10), B.Date,23) FROM #Member AS A, ( SELECT A.FMemId,MAX(FTime) AS Date FROM TMemberTransHist AS A WITH(NOLOCK),#Member AS B WHERE A.FMemId = B.FMemId GROUP BY A.FMemId ) AS B WHERE A.FMemid = B.FMemId IF EXISTS(SELECT TOP 1 1 FROM TMemberOldTransHist WITH(NOLOCK)) BEGIN --最后消费日期 UPDATE A SET A.FLastDate = CONVERT(varchar(10), B.Date,23) FROM #Member AS A, ( SELECT A.FMemId,MAX(FTime) AS Date FROM TMemberOldTransHist AS A WITH(NOLOCK),#Member AS B WHERE A.FMemId = B.FMemId AND B.FLastDate = '' GROUP BY A.FMemId ) AS B WHERE A.FMemid = B.FMemId END IF @FromLastDate != '' BEGIN --排除最后消费日期 DELETE #Member WITH(ROWLOCK) WHERE FLastDate NOT BETWEEN @FromLastDate AND @ToLastDate END IF @NotComeDay != 0 BEGIN DECLARE @NoComeDate VARCHAR(10) SELECT @NoComeDate = CONVERT(VARCHAR(10) ,(SELECT DATEADD(d,-@NotComeDay,getdate())),23) DELETE #Member WITH(ROWLOCK) WHERE FLastDate > @NoComeDate END --最后消费日期 IF @AfterLastDate != '' BEGIN DELETE #Member WHERE FLastDate > @AfterLastDate END --消费总次数 UPDATE A SET A.FConsTimes = B.Times FROM #Member AS A, ( SELECT FMemId,SUM(1) AS Times FROM ( SELECT FMemId,SUM(1) AS Times FROM TMemberTransHist WITH(NOLOCK) WHERE FBillType = 'XF' AND (@FromStatDate = '' OR FTime BETWEEN @FromStatDate AND @ToStatDate) GROUP BY FMemId,FTransId) AS C GROUP BY FMemId ) AS B WHERE A.FMemId = B.FMemId IF EXISTS(SELECT TOP 1 1 FROM TMemberOldTransHist WITH(NOLOCK)) BEGIN UPDATE A SET A.FConsTimes = ISNULL(A.FConsTimes,0) + B.Times FROM #Member AS A, ( SELECT FMemId,SUM(1) AS Times FROM TMemberOldTransHist WITH(NOLOCK) WHERE FBillType = 'DR.XF' AND (@FromStatDate = '' OR FTime BETWEEN @FromStatDate AND @ToStatDate) GROUP BY FMemId,FTransNo ) AS B WHERE A.FMemId = B.FMemId END --排除消费次数 IF @FromConsTimes IS NOT NULL BEGIN DELETE #Member WITH(ROWLOCK) WHERE FConsTimes NOT BETWEEN @FromConsTimes AND @ToConsTimes END IF db_name() = 'JiNan_JinZhi' BEGIN --消费和充值总金额 UPDATE A SET A.FConsAmt = B.ConsAmt, A.FRchgAmt = B.RchgAmt FROM #Member AS A, ( SELECT FMemId,SUM(ConsAmt) AS ConsAmt,SUM(RchgAmt) AS RchgAmt FROM (SELECT A.FMemId,SUM(CASE WHEN FBillType = 'XF' THEN FAmount ELSE 0 END) AS ConsAmt, SUM(CASE WHEN FBizType IN('CourseSale_不算','CardRchg','CardSale','CoursePackSale_不算') THEN FAmount ELSE 0 END) AS RchgAmt FROM TMemberTransHist AS A WITH(NOLOCK),#Member AS C WHERE A.FMemId = C.FMemId AND FBillType IN('XF','KK') AND (@FromStatDate = '' OR FTime BETWEEN @FromStatDate AND @ToStatDate) AND EXISTS(SELECT 1 FROM #MemberCard AS B WHERE B.FCardId = A.FCardId) GROUP BY A.FMemId,FBillType,FBizType) AS T GROUP BY FMemId ) AS B WHERE A.FMemId = B.FMemId END ELSE BEGIN --消费和充值总金额 DECLARE @BizType_CourseSale VARCHAR(100) DECLARE @BizType_CoursePackSale VARCHAR(100) DECLARE @BizType_CardSale VARCHAR(100) DECLARE @BizType_CardRchg VARCHAR(100) IF @ShowCardInfo = 1 BEGIN SELECT @BizType_CardSale = 'CardSale' SELECT @BizType_CardRchg = 'CardRchg' END ELSE BEGIN SELECT @BizType_CardSale = 'NAN' SELECT @BizType_CardRchg = 'NAN' END IF @ShowCourseInfo = 1 BEGIN SELECT @BizType_CourseSale = 'CourseSale' SELECT @BizType_CoursePackSale = 'CoursePackSale' END ELSE BEGIN SELECT @BizType_CourseSale = 'NAN' SELECT @BizType_CoursePackSale = 'NAN' END UPDATE A SET A.FConsAmt = B.ConsAmt, A.FRchgAmt = B.RchgAmt FROM #Member AS A, ( SELECT FMemId,SUM(ConsAmt) AS ConsAmt,SUM(RchgAmt) AS RchgAmt FROM (SELECT A.FMemId,SUM(CASE WHEN FBillType = 'XF' THEN FAmount ELSE 0 END) AS ConsAmt, SUM(CASE WHEN FBizType IN(@BizType_CourseSale,@BizType_CardRchg,@BizType_CardSale,@BizType_CoursePackSale) THEN FAmount ELSE 0 END) AS RchgAmt FROM TMemberTransHist AS A WITH(NOLOCK),#Member AS C WHERE A.FMemId = C.FMemId AND FBillType IN('XF','KK') AND (@FromStatDate = '' OR FTime BETWEEN @FromStatDate AND @ToStatDate) GROUP BY A.FMemId,FBillType,FBizType) AS T GROUP BY FMemId ) AS B WHERE A.FMemId = B.FMemId END --总储值消费 IF @ShowCardConsAmt = 1 BEGIN UPDATE A SET A.FCardConsAmt = B.FConsAmt FROM #Member AS A,( SELECT A.FMemId,SUM(A.FOutAmount) AS FConsAmt FROM TMemberCardAcctHist AS A ,#Member AS B WHERE B.FMemId = A.FMemId AND (@FromStatDate = '' OR FTime BETWEEN @FromStatDate AND @ToStatDate) AND A.FBizType = 'XF' GROUP BY A.FMemId) AS B WHERE A.FMemId = B.FMemId IF EXISTS(SELECT 1 FROM TMemberCardOldAcctHist) BEGIN UPDATE A SET A.FCardConsAmt = ISNULL(A.FCardConsAmt,0) + B.FConsAmt FROM #Member AS A,( SELECT A.FMemId,SUM(A.FOutAmount) AS FConsAmt FROM TMemberCardOldAcctHist AS A ,#Member AS B WHERE B.FMemId = A.FMemId AND A.FBizType = '消费' AND (@FromStatDate = '' OR FTime BETWEEN @FromStatDate AND @ToStatDate) GROUP BY A.FMemId) AS B WHERE A.FMemId = B.FMemId END --期限卡也要算进去 UPDATE A SET A.FCardConsAmt = A.FCardConsAmt + B.FConsAmt FROM #Member AS A,( SELECT A.FMemId,SUM(B.FPayAmt)AS FConsAmt FROM TMemberTransHistPayment AS B,TMemberTransHist AS A,#Member AS M WHERE A.FMemId = B.FMemId AND M.FMemId = B.FMemId AND A.FBillType = B.FBillType AND A.FTransId = B.FTransId AND A.FTransDetailId = B.FTransDetailId AND B.FPayTypeId = 7 AND A.FBillType = 'XF' AND (@FromStatDate = '' OR FTime BETWEEN @FromStatDate AND @ToStatDate) GROUP BY A.FMemId ) AS B WHERE A.FMemId = B.FMemId END IF EXISTS(SELECT TOP 1 1 FROM TMemberOldTransHist WITH(NOLOCK)) BEGIN --消费和充值总金额 UPDATE A SET A.FConsAmt = ISNULL(A.FConsAmt,0) + B.ConsAmt, A.FRchgAmt = ISNULL(A.FRchgAmt,0) + B.RchgAmt FROM #Member AS A, ( SELECT FMemId,SUM(ConsAmt) AS ConsAmt,SUM(RchgAmt) AS RchgAmt FROM (SELECT A.FMemId,SUM(CASE WHEN FBillType = 'DR.XF' THEN FAmount ELSE 0 END) AS ConsAmt, SUM(CASE WHEN FBizType IN('DR.CardSale','DR.CardRchg') THEN FAmount ELSE 0 END) AS RchgAmt FROM TMemberOldTransHist AS A WITH(NOLOCK),#Member AS C WHERE A.FMemId = C.FMemId AND FBillType IN('DR.XF','DR.KK') AND (@FromStatDate = '' OR FTime BETWEEN @FromStatDate AND @ToStatDate) GROUP BY A.FMemId,FBillType) AS T GROUP BY FMemId ) AS B WHERE A.FMemId = B.FMemId END --排除消费充值总额 IF (@FromConsAmt IS NOT NULL AND @ToConsAmt IS NOT NULL) BEGIN DELETE #Member WHERE FConsAmt NOT BETWEEN @FromConsAmt AND @ToConsAmt END IF (@FromRchgAmt IS NOT NULL AND @ToRchgAmt IS NOT NULL) BEGIN IF @FromRchgAmt = 0 BEGIN SET @FromRchgAmt = 0.001 END DELETE #Member WHERE FRchgAmt NOT BETWEEN @FromRchgAmt AND @ToRchgAmt END --获取最喜爱的员工 UPDATE A SET A.FFavoriteEmpName = CASE WHEN A.FFavoriteEmpName IS NULL THEN E.FName ELSE A.FFavoriteEmpName END FROM #Member AS A, ( SELECT TOP 1000000000 A.FMemId,C.FEmpId,SUM(1) AS SUM FROM #Member AS A,TConsumeBill AS T WITH(NOLOCK),TConsumeItemEmployee AS C WITH(NOLOCK) WHERE A.FMemId = T.FMemId AND T.FBillId = C.FBillId AND C.FEmpId != 0 AND C.FDispatchMode = 10001 AND T.FState = 1 GROUP BY A.FMemId,C.FEmpId ORDER BY A.FMemId,SUM(1) DESC )AS B ,TEmployee AS E WHERE A.FMemId = B.FMemId AND B.FEmpId = E.FId AND (@FavoriteEmpId = 0 OR B.FEmpId = @FavoriteEmpId) --排除最喜爱的员工 IF @FavoriteEmpId != 0 BEGIN DELETE #Member WHERE FFavoriteEmpName = '' OR FFavoriteEmpName IS NULL END --最后再清理一次卡 DELETE A FROM #MemberCard AS A WHERE NOT EXISTS(SELECT 1 FROM #Member AS B WHERE A.FMemId = B.FMemId) --卡类别 UPDATE A SET A.FCardTypeName = B.FNo + '-' + B.FName FROM #MemberCard AS A,TMemberCardType AS B WHERE A.FCardTypeId = B.FId AND A.FCardKind = 'Money' --疗程 UPDATE A SET A.FCardTypeName = B.FNo + '-' + B.FName FROM #MemberCard AS A,TCourse AS B WHERE A.FCardTypeId = B.FId AND A.FCardKind = 'Course' AND B.FName != '' --项目 UPDATE A SET A.FCardTypeName = B.FNo + '-' + B.FName FROM #MemberCard AS A,TItem AS B WHERE A.FItemId = B.FId AND A.FCardKind = 'Course' AND A.FCardTypeName IN('-','') AND A.FCardTypeName = '' --客单价 UPDATE #Member SET FConsPrice = FConsAmt / FConsTimes WHERE FConsTimes > 0 --获取负责员工 /* UPDATE A SET A.FSaleEmpName = E.FName FROM #Member AS A,TEmployee AS E WITH(NOLOCK) WHERE E.FId = A.FChargeEmpId */ UPDATE A SET A.FBizEmpName = E.FName FROM #MemberCard AS A,TEmployee AS E WITH(NOLOCK) WHERE E.FId = A.FBizEmpId SELECT FMemId AS MemId, FMemNo AS MemNo, A.FName AS Name, A.FMemMemo AS MemMemo, ISNULL(C.FAbbr,'') AS MemCompName, FGender AS Gender, FMobilePhone AS MobilePhone, FBirthday AS Birthday, FBirthdayType AS BirthdayType,FJoinDate AS JoinDate, FPoint AS Point, FArrear AS Arrear, FConsTimes AS ConsTimes,FCardConsAmt AS CardConsAmt, FConsAmt AS ConsAmt, FRchgAmt AS RchgAmt, FLastDate AS LastDate, FConsArrear AS ConsArrear, FCourseArrear AS CourseArrear,FConsPrice AS ConsPrice, FFavoriteEmpName AS FavoriteEmpName,FSaleEmpName AS SaleEmpName FROM #Member AS A LEFT JOIN TCompany AS C WITH(NOLOCK) ON A.FCompId = C.FId ORDER BY FMemNo SELECT A.FMemId AS MemId,A.FCardId AS CardId,A.FCardNo AS CardNo, B.FMemNo AS MemNo, A.FCardMemo AS CardMemo,ISNULL(C.FAbbr,'') AS CardCompName, A.FCardTypeNo AS CardTypeNo,A.FCardTypeName AS CardTypeName, A.FCreateDate AS CardCreateDate,A.FExpiredDate AS CardExpiredDate,CASE WHEN A.FCalcMode = 2 THEN '不限次' ELSE CAST(CAST( A.FRemainTimes AS FLOAT) AS VARCHAR(20)) END AS CardRemainTimes, A.FPurAmount AS CardPurAmount, A.FState AS CardState,A.FBizEmpName AS BizEmpName FROM #MemberCard AS A INNER JOIN #Member AS B ON A.FMemId = B.FMemId LEFT JOIN TCompany AS C WITH(NOLOCK) ON A.FCompId = C.FId ORDER BY FCardNo --获取卡余额表 -- IF @ShowCardInfo = 1 -- BEGIN DECLARE @CourseName VARCHAR(30) EXEC PGetBizParameter 1,'CourseDefineName',@CourseName output SELECT @CourseName = @CourseName + '金额' SELECT B.FCardId AS CardId, B.FCardNo AS CardNo,I.FNo AS AcctNo,CASE A.FAcctId WHEN 6 THEN @CourseName ELSE I.FName END AS AcctName ,A.FBalance AS Balance,A.FArrear AS Arrear FROM #MemberCardAccount AS A WITH(NOLOCK),#MemberCard AS B,TPayAccount AS I ,#Member AS M WHERE A.FCardId = B.FCardId AND A.FAcctId = I.FId AND M.FMemId = B.FMemId -- AND B.FCardKind = 'Money' ORDER BY B.FCardNo CREATE TABLE #TransHist ( FPayTypeId BIGINT, FPayTypeNo VARCHAR(100), FPayTypeName VARCHAR(100), FPayAmt DECIMAL(18,6) ) -- SELECT * INTO ##Member FROM #Member INSERT #TransHist(FPayTypeId,FPayTypeNo,FPayTypeName,FPayAmt) SELECT A.FPayTypeId,P.FNo,P.FName,SUM(A.FPayAmt) FROM TMemberTransHistPayment AS A INNER JOIN TMemberTransHist AS B ON B.FMemId = A.FMemId AND A.FTransId = B.FTransId AND A.FTransDetailId = B.FTransDetailId AND A.FBillType = B.FBillType LEFT JOIN TPayAccount AS P ON P.FId = A.FPayTypeId WHERE A.FMemId IN(SELECT FMemId FROM #Member) AND A.FBillType = 'XF' GROUP BY A.FPayTypeId,P.FName,P.FNo SELECT FPayTypeName as 支付方式,FPayAmt AS 金额 FROM #TransHist ORDER BY FPayTypeNo select sum(FAmount) FROM TMemberTransHist WHERE FMemId IN(SELECT FMemId FROM ##Member) AND FBillType = 'XF' select sum(FPayAmt) FROM TMemberTransHistPayment WHERE FMemId IN(SELECT FMemId FROM ##Member) AND FBillType = 'XF' /* SELECT * FROM TMemberTransHist WHERE FTransDetailId NOT IN(SELECT FTransDetailId FROM TMemberTransHistPayment) AND FBillType = 'XF' INSERT TMemberTransHistPayment(FMemId,FCardId,FBillType,FTransId,FTransDetailId,FPayTypeId,FPayAmt) SELECT A.FMemId,B.FCardId,'XF',A.FTransid,B.FDetailId,FPayTypeId,FPayAmt FROM TCOnsumeGoodsPayment AS B,TMemberTransHist AS A WHERE A.FTransid = B.FBillId AND A.FTransDetailId = B.FDetailId AND A.FTransDetailId NOT IN(SELECT FTransDetailId FROM TMemberTransHistPayment) SELECT count(*) FROM TMemberTransHist AS A, (SELECT FMemId,FTransId,FTransDetailId,SUM(FPayAmt)FPayAmt FROM TMemberTransHistPayment GROUP BY FMemId,FTransId,FTransDetailId) AS B WHERE A.FTransId = B.FTransId AND A.FMemId = B.FMemId AND A.FTransDetailId = B.FTransDetailId AND A.FBillType = 'XF' AND A.FMemId IN(SELECT FMemId FROM ##Member) SELECT COUNT(1) FROM TMemberTransHist WHERE FMemId IN(SELECT FMemId FROM ##Member) SELECT COUNT(1) FROM TMemberTransHist WHERE FMemId IN(SELECT FMemId FROM ##Member) */ /* UPDATE A SET A.FMemId = B.FMemId FROM TMemberTransHistPayment AS A, TMemberTransHist AS B WHERE A.FTransId = B.FTransiD AND A.FTransDetailId = B.FTransDetailId AND A.FMemId != B.FMemId */ -- SELECT * FROM TConsumeBill -- WHERE FBillId NOT IN(SELECT FTransId FROM TMemberTransHistPayment) -- AND FMemId != 0 /* select * from ggm01 where gga00c = '005' order by gga02d desc select * from TMemberTransHist WHERE FTransId NOT IN(SELECT FTransid FROM TMemberTransHistPayment) AND FBillType = 'XF' SELECT A.FTransId,A.FAmount, B.FPayAmt FROM TMemberTransHist AS A,(SELECT FTransid,FTransDetailId,FBillType,SUM(FPayAmt) AS FPayAmt FROM TMemberTransHistPayment GROUP BY FTransDetailId,FTransId,FBillType) AS B WHERE A.FTransId = B.FTransId AND A.FBillType= B.FBillType AND A.FTransDetailId = B.FTransDetailId AND A.FAmount != B.FPayAmt SELECT * FROM TMemberTransHist WHERE FTransId = 5629196643205643967 SELECT * FROM TMemberTransHistPayment WHERE FTransId = 5629196643205643967 SELECT * FROM TConsumeItemPayment WHERE FBillId = 5629196643205643967 SELECT * FROM TConsumeItem WHERE FBillId = 5629196643205643967 UPDATE A SET A.FAmount = B.FPayAmt FROM TConsumeItem AS A, (SELECT FBillId,FDetailId ,SUM(FPayAmt) AS FPayAmt FROM TConsumeItemPayment GROUP BY FBillId,FDetailId ) AS B WHERE A.FBillId = B.FBillId AND A.FId = B.FDetailId AND A.FAmount != B.FPayAmt UPDATE A SET A.FAmount = B.FAmount FROM TMemberTransHist AS A,TConsumeItem AS B WHERE A.FTransDetailId = B.FId AND A.FAmount != B.FAmount AND A.FBIlLType = 'XF' */ -- SELECT * FROM TMemberTransHistPayment -- WHERE FTransId NOT IN(SELECT ) -- EXEC PRptMemberReport 'Company="005"CompNo="005"CompSource="2"FromStatDate="2016-07-01"ToStatDate="9999-01-01"ShowCardInfo="1"CardState="1"CardExpired="0"ShowCourseInfo="1"ShowAllMember="1"IsVisitor="0"LoginUserNo="System"ShowCardConsAmt="0"' --SELECT * FROM TConsumeBill WHERE FCompId = dbo.FNCompId('005')ORDER BY FBillTime -- END -- ELSE -- BEGIN -- SELECT '' AS CardNo ,'' AS AcctNo ,'' AS AcctName,CAST(0 AS DECIMAL(18,6)) AS Balance ,CAST(0 AS DECIMAL(18,6)) AS Arrear --END -- select * from TMember --83807855 --15589888082 --SELECT * FROM TMemberTransHist --DECLARE @IParam VARCHAR(8000) --SELECT @IParam = -- ' -- FromMemNo="" -- ToMemNo="888" -- CompanyForCard="1" -- CardTypeNo="A" -- Company="001,002,003,A001" -- MobilePhone="" -- FromBirthday="" -- ToBirthday="1-1209" -- FromLastDate="2012-01-01" -- ToLastDate="2015-05-01" -- ' --EXEC PRptMemberReport @IParam END GO SELECT * FROM TCompany W DROP TABLE #Card SELECT FCardId INTO #Card FROM TMemberCard WHERE FCompId = 3472334686330552320 AND FState = 1 SELECT FAcctId,P.FName, SUM(FInAmount) AS 总充值, SUM(FOutAmount) AS 总消费, SUM(FInAmount) - SUM(FOutAmount) as 剩余 FROM TMemberCardOldAcctHist LEFT JOIN TPayAccount AS P ON P.FId = TMemberCardOldAcctHist.FAcctId WHERE FCardId IN(SELECT FCardId FROM #Card) AND FTime > '2016-04-01' AND FAcctId IN(3,5295626290595979152) GROUP BY FAcctId,p.FName use chongqing_qijia SELECT FAcctId,P.FName, SUM(FInAmount) AS 总充值 , SUM(FOutAmount) AS 总消费 , SUM(FInAmount) - SUM(FOutAmount) as 剩余 FROM TMemberCardAcctHist LEFT JOIN TPayAccount AS P ON P.FId = TMemberCardAcctHist.FAcctId WHERE FCardId IN(SELECT FCardId FROM #Card) AND FAcctId IN(3,5295626290595979152) AND FBillType NOT IN('XF','KK') AND FTime < '2018-03-20 23:59:59' GROUP BY FAcctId,p.FName SELECT * FROM ( SELECT B.FCardId,B.FAcctId,SUM(B.FBalance) AS FBalance FROM TMemberCardAcctHist AS B, ( SELECT FCardId,FAcctId,MAX(FRecordTime) AS FRecordTime FROM TMemberCardAcctHist AS H WHERE FTime <= '2018-03-20 23:59:59' AND FCardId IN(SELECT FCardId FROM #Card) GROUP BY FCardId,FAcctId ) AS C WHERE B.FCardId = C.FCardId AND B.FAcctId = C.FAcctId AND B.FRecordTime = C.FRecordTime AND B.FAcctId = 3 GROUP BY B.FAcctId ,B.FCardId ) AS A , ( SELECT FCardId,FAcctId, SUM(FInAmount) - SUM(FOutAmount) as FBalance FROM TMemberCardAcctHist WHERE FCardId IN(SELECT FCardId FROM #Card) AND FAcctId IN(3) AND FTime < '2018-03-20 23:59:59' GROUP BY FCardId,FAcctId ) AS B WHERE A.FCardId =B.FCardId AND A.FAcctId = B.FAcctId AND A.FBalance != B.FBalance AND A.FCardId = 4640664311468382143 SELECT ' EXEC PRecalcCardAccHistBalance ' + CAST(FCardId AS VARCHAR) FROM #Card SELECT * FROM TMemberCardAcctHist WHERE FCardId = 4640664311468382143 AND FAcctId IN(3) ORDER BY FTime ASC AND FRecordTime = '2018-04-20 12:24:38.297' SELECT FCardId,FAcctId, SUM(FInAmount) - SUM(FOutAmount) as FBalance FROM TMemberCardAcctHist WHERE FCardId IN(SELECT FCardId FROM #Card) AND FAcctId IN(3) AND FTime < '2018-03-20 23:59:59' AND FCardId = 4640664311468382143 GROUP BY FCardId,FAcctId select 46387 + 112433.900000= 153798 SELECT * FROM TMemberCardAcctHist WHERE FBillType NOT IN('XF','KK','DR') AND FCardId IN(SELECT FCardId FROM #Card)