ALTER PROCEDURE dbo.PRptMemberReport07 ( @IParam VARCHAR(8000) ) AS BEGIN DECLARE @CompNo VARCHAR(8000) DECLARE @FromPurDate DATETIME DECLARE @ToPurDate DATETIME DECLARE @FromExprDate DATETIME DECLARE @ToExprDate DATETIME DECLARE @FromItemNo VARCHAR(30) DECLARE @ToItemNo VARCHAR(30) DECLARE @IncExpired BIT SELECT @CompNo = dbo.FNGetParamValue(@IParam,'CompNo'); SELECT @IncExpired = ISNULL(dbo.FNGetParamValue(@IParam,'IncExpired'),0); DECLARE @Companys TABLE ( CompId BIGINT, CompNo VARCHAR(20), CompName VARCHAR(50) ) INSERT @Companys SELECT A.FId,A.FNo,A.FAbbr FROM TCompany AS A WITH(NOLOCK), dbo.FNStrSplit(@CompNo) AS B WHERE A.FNo = B.Field IF @@ROWCOUNT = 0 BEGIN RETURN END CREATE TABLE #Result ( CompId BIGINT, CompName VARCHAR(100), MemberTotal INT, MoneyCardTotal INT, CourseCardTotal INT, LiveMemberTotal2 INT, LiveMoneyCardTotal2 INT, LiveCourseCardTotal2 INT, LiveMemberTotal1 INT, LiveMoneyCardTotal1 INT, LiveCourseCardTotal1 INT, TotalBalance DECIMAL(18,6), MoneyCardBalance DECIMAL(18,6), CourseCardBalance DECIMAL(18,6), SleepTotalBalance2 DECIMAL(18,6), SleepTotalBalance1 DECIMAL(18,6) ) INSERT #Result(CompId,CompName) SELECT CompId,CompNo + '-' + CompName FROM @Companys SELECT A.FCompId,A.FMemId,A.FCardId ,CAST(0 AS BIT) AS FLive2,CAST(0 AS BIT) AS FLive1 INTO #MCard FROM TMemberCard AS A WITH(NOLOCK),TMember AS B WITH(NOLOCK) ,@Companys AS C WHERE A.FDeleted = 0 AND B.FDeleted = 0 AND B.FState = 1 AND C.CompId = A.FCompId AND A.FMemId = B.FMemId AND (@IncExpired = 0 OR A.FExpiredDate <= CONVERT(VARCHAR,GETDATE(),23)) SELECT A.FCompId,A.FMemId,A.FId AS FCardId ,A.FRemainPurAmount AS FRemainPurAmount,CAST(0 AS BIT) AS FLive2,CAST(0 AS BIT) AS FLive1 INTO #CCard FROM TMemberCourse AS A WITH(NOLOCK),TMember AS B WITH(NOLOCK) ,@Companys AS C WHERE A.FDeleted = 0 AND B.FDeleted = 0 AND B.FState = 1 AND C.CompId = A.FCompId AND A.FMemId = B.FMemId AND (@IncExpired = 0 OR A.FExpiredDate <= CONVERT(VARCHAR,GETDATE(),23)) --会员数量 SELECT A.FCompId,A.FMemId ,CAST(0 AS BIT) AS FLive2,CAST(0 AS BIT) AS FLive1 INTO #Member FROM ( SELECT FCompId,FMemId FROM #MCard GROUP BY FCompId,FMemId UNION ALL SELECT FCompId,FMemId FROM #CCard GROUP BY FCompId,FMemId ) AS A GROUP BY A.FCompId,A.FMemId UPDATE A SET A.MemberTotal = B.FTotal FROM #Result AS A,( SELECT A.FCompId,COUNT(1) AS FTotal FROM ( SELECT * FROM #Member ) AS A GROUP BY FCompId ) AS B WHERE A.CompId = B.FCompId --储值卡数量 UPDATE A SET A.MoneyCardTotal = B.FTotal FROM #Result AS A,(SELECT FCompId,COUNT(1) AS FTotal FROM #MCard GROUP BY FCompId) AS B WHERE A.CompId = B.FCompId --疗程卡数量 UPDATE A SET A.CourseCardTotal = B.FTotal FROM #Result AS A,(SELECT FCompId,COUNT(1) AS FTotal FROM #CCard GROUP BY FCompId) AS B WHERE A.CompId = B.FCompId --两年内消费过的储值卡,根据账户历史判断 DECLARE @Time1 DATETIME DECLARE @Time2 DATETIME SELECT @Time1 = DATEADD(YEAR,-1,GETDATE()),@Time2 = DATEADD(YEAR,-2,GETDATE()) --SELECT * FROM #Member UPDATE A SET A.FLive2 = 1 FROM #MCard AS A WHERE EXISTS(SELECT 1 FROM TMemberCardAcctHist AS B WITH(NOLOCK) WHERE B.FCardId = A.FCardId AND B.FTime > @Time2 AND B.FBillType IN('XF','KK')) UPDATE A SET A.FLive2 = 1 FROM #MCard AS A WHERE EXISTS(SELECT 1 FROM TMemberCardOldAcctHist AS B WITH(NOLOCK) WHERE B.FCardId = A.FCardId AND B.FTime > @Time2 ) UPDATE A SET A.FLive1 = 1 FROM #MCard AS A WHERE EXISTS(SELECT 1 FROM TMemberCardAcctHist AS B WITH(NOLOCK) WHERE B.FCardId = A.FCardId AND B.FTime > @Time1 AND B.FBillType IN('XF','KK')) UPDATE A SET A.FLive1 = 1 FROM #MCard AS A WHERE EXISTS(SELECT 1 FROM TMemberCardOldAcctHist AS B WITH(NOLOCK) WHERE B.FCardId = A.FCardId AND B.FTime > @Time1 ) UPDATE A SET A.FLive1 = 1 FROM #MCard AS A WHERE EXISTS(SELECT 1 FROM TMemberCourseLog AS B WITH(NOLOCK) WHERE B.FCardId = A.FCardId AND B.FTime > @Time1 AND B.FBillType IN('XF','KK')) UPDATE A SET A.FLive2 = 1 FROM #MCard AS A WHERE EXISTS(SELECT 1 FROM TMemberCourseLog AS B WITH(NOLOCK) WHERE B.FCardId = A.FCardId AND B.FTime > @Time2 AND B.FBillType IN('XF','KK')) UPDATE A SET A.FLive1 = 1 FROM #Member AS A,#MCard AS B WHERE A.FMemId = B.FMemId AND B.FLive1 = 1 UPDATE A SET A.FLive1 = 1 FROM #Member AS A,#CCard AS B WHERE A.FMemId = B.FMemId AND B.FLive1 = 1 UPDATE A SET A.FLive2 = 1 FROM #Member AS A,#MCard AS B WHERE A.FMemId = B.FMemId AND B.FLive2 = 1 UPDATE A SET A.FLive2 = 1 FROM #Member AS A,#CCard AS B WHERE A.FMemId = B.FMemId AND B.FLive2 = 1 --更新活跃会员数2 UPDATE A SET A.LiveMemberTotal2 = B.FLive2 FROM #Result AS A,(SELECT FCompId,COUNT(1) AS FLive2 FROM #Member WHERE FLive2 = 1 GROUP BY FCompId) AS B WHERE A.CompId = B.FCompId --更新活跃会员数1 UPDATE A SET A.LiveMemberTotal1 = B.FLive1 FROM #Result AS A,(SELECT FCompId,COUNT(1) AS FLive1 FROM #Member WHERE FLive1 = 1 GROUP BY FCompId) AS B WHERE A.CompId = B.FCompId --更新活跃卡2 UPDATE A SET A.LiveMoneyCardTotal2 = B.FLive2 FROM #Result AS A,(SELECT FCompId,COUNT(1) AS FLive2 FROM #MCard WHERE FLive2 = 1 GROUP BY FCompId) AS B WHERE A.CompId = B.FCompId --更新活跃卡1 UPDATE A SET A.LiveMoneyCardTotal1 = B.FLive1 FROM #Result AS A,(SELECT FCompId,COUNT(1) AS FLive1 FROM #MCard WHERE FLive1 = 1 GROUP BY FCompId) AS B WHERE A.CompId = B.FCompId --储值卡余额 UPDATE A SET A.MoneyCardBalance = B.FBalance FROM #Result AS A,(SELECT A.FCompId,SUM(C.FBalance) AS FBalance FROM #MCard AS A,TMemberCardAccount AS C WHERE C.FCardId = A.FCardId AND C.FAcctId = 3 GROUP BY A.FCompId ) AS B WHERE A.CompId = B.FCompId --疗程卡余额 UPDATE A SET A.CourseCardBalance = B.FBalance FROM #Result AS A,(SELECT A.FCompId,SUM(A.FRemainPurAmount) AS FBalance FROM #CCard AS A GROUP BY A.FCompId ) AS B WHERE A.CompId = B.FCompId --总余额 UPDATE #Result SET TotalBalance = CourseCardBalance + MoneyCardBalance --休眠的总余额 UPDATE A SET A.SleepTotalBalance2 = B.FBalance FROM #Result AS A,(SELECT A.FCompId,SUM(C.FBalance) AS FBalance FROM #MCard AS A,TMemberCardAccount AS C WHERE A.FLive2 = 0 AND C.FCardId = A.FCardId AND C.FAcctId = 3 GROUP BY A.FCompId ) AS B WHERE A.CompId = B.FCompId --休眠1的总余额 UPDATE A SET A.SleepTotalBalance1 = B.FBalance FROM #Result AS A,(SELECT A.FCompId,SUM(C.FBalance) AS FBalance FROM #MCard AS A,TMemberCardAccount AS C WHERE A.FLive1 = 0 AND C.FCardId = A.FCardId AND C.FAcctId = 3 GROUP BY A.FCompId ) AS B WHERE A.CompId = B.FCompId SELECT * FROM #Result ORDER BY CompName SELECT (SELECT COUNT(1) FROM (SELECT FMemId FROM #Member GROUP BY FMemId) AS A) AS MemberTotal, SUM(MoneyCardTotal) AS MoneyCardTotal, SUM(CourseCardTotal) AS MoneyCardTotal, (SELECT COUNT(1) FROM (SELECT FMemId FROM #Member WHERE FLive2 = 1 GROUP BY FMemId) AS A) AS LiveMemberTotal2, (SELECT COUNT(1) FROM (SELECT FMemId FROM #Member WHERE FLive1 = 1 GROUP BY FMemId) AS A) AS LiveMemberTotal1, SUM(LiveMoneyCardTotal2) AS LiveMoneyCardTotal2, SUM(LiveMoneyCardTotal1) AS LiveMoneyCardTotal1, SUM(TotalBalance) AS TotalBalance, SUM(MoneyCardBalance) AS MoneyCardBalance, SUM(CourseCardBalance) AS CourseCardBalance, SUM(SleepTotalBalance1) AS SleepTotalBalance1, SUM(SleepTotalBalance2) AS SleepTotalBalance2 FROM #Result END GO EXEC dbo.PRptMemberReport07 'CompNo="001,002"'