You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
sql-tools/MemberReport201.sql

181 lines
6.2 KiB

USE zhengzhou_lishuangshuang
GO
/*
EXEC PUpdateReportMapping 'MemberReport20','PRptMemberReport20','运转顾客分析表'
INSERT TFunction(FName,FDisplayName,FTypeName,FParentName,FGroupName,FCategory,FVisible,FOrder)
SELECT 'RMemberReport20','运转顾客分析表','','','MRReport',2,1,2
INSERT TFunctionAccessDict(FFunctionName,FAccessKey,FDefaultValue)
SELECT 'RMemberReport20' ,'Basically',1
INSERT TRoleAccess(FId,FRoleId,FType,FFunctionName,FAccessKey,FValue)
SELECT dbo.FNNewId(),B.FId,2 AS FType,'RMemberReport20','Basically',1
FROM TRole AS B
WHERE B.FId > 1
*/
IF object_id('dbo.PRptMemberReport20','P') IS NOT NULL
BEGIN
DROP PROCEDURE dbo.PRptMemberReport20
END
GO
CREATE PROCEDURE dbo.PRptMemberReport20
(
@Param VARCHAR(100)
)
AS
BEGIN
DECLARE @FromMonth CHAR(10)
DECLARE @ToMonth CHAR(10)
DECLARE @CompNo VARCHAR(1000) --会员门店
DECLARE @LevelId BIGINT
SELECT @LevelId = ISNULL(dbo.FNGetParamValue(@Param,'Level'),0)
SELECT @FromMonth = ISNULL(dbo.FNGetParamValue(@Param,'FromMonth'),'')
SELECT @ToMonth = ISNULL(dbo.FNGetParamValue(@Param,'ToMonth'),'')
SELECT @CompNo = ISNULL(dbo.FNGetParamValue(@Param,'CompNo'),'')
CREATE TABLE #Company
(
FCompId BIGINT,
FCompNo VARCHAR(50),
FCompName VARCHAR(50)
)
INSERT #Company(FCompId,FCOmpNo,FCompName)
SELECT FId,FNo,FAbbr
FROM TCompany
WHERE FNo IN(SELECT Field FROM dbo.FNStrSplit(@CompNo))
CREATE TABLE #Result
(
CompId BIGINT,
MemId BIGINT,
MemName VARCHAR(100),
MemNo VARCHAR(50),
Mobile VARCHAR(21),
CreateDate VARCHAR(10),
StateDesc VARCHAR(50),
EmpId14001 BIGINT NOT NULL DEFAULT 0,
EmpId14002 BIGINT NOT NULL DEFAULT 0,
EmpId14003 BIGINT NOT NULL DEFAULT 0,
EmpId14004 BIGINT NOT NULL DEFAULT 0,
EmpName14001 VARCHAR(100) NOT NULL DEFAULT '' ,
EmpName14002 VARCHAR(100) NOT NULL DEFAULT '',
EmpName14003 VARCHAR(100) NOT NULL DEFAULT '',
EmpName14004 VARCHAR(100) NOT NULL DEFAULT '',
)
INSERT #Result(MemId,CompId,MemName,MemNo,Mobile,CreateDate,StateDesc)
SELECT M.FMemId,M.FCompId,M.FName,M.FMemNo,M.FMobilePhone,CONVERT(VARCHAR, M.FCreateDate,23),''
FROM TMember AS M
INNER JOIN #Company AS C ON C.FCompId = M.FCompId
WHERE M.FState = 1
AND M.FIsVisitor = 0
UPDATE A
SET A.EmpId14001 = CASE WHEN B.FWorkType = 14001 THEN B.FEmpId ELSE EmpId14001 END,
A.EmpId14002 = CASE WHEN B.FWorkType = 14002 THEN B.FEmpId ELSE EmpId14002 END,
A.EmpId14003 = CASE WHEN B.FWorkType = 14003 THEN B.FEmpId ELSE EmpId14003 END,
A.EmpId14004 = CASE WHEN B.FWorkType = 14004 THEN B.FEmpId ELSE EmpId14004 END
--A.FEmpName14001 = E.FName
FROM #Result AS A,TMemberEmployee AS B
WHERE A.MemId = B.FMemId
AND B.FWorkKey = 'RegWorkType'
UPDATE A SET A.EmpName14001 = B.FName FROM #Result AS A,TEmployee AS B WHERE B.FId = A.EmpId14001 AND A.EmpId14001 != 0
UPDATE A SET A.EmpName14002 = B.FName FROM #Result AS A,TEmployee AS B WHERE B.FId = A.EmpId14002 AND A.EmpId14002 != 0
UPDATE A SET A.EmpName14003 = B.FName FROM #Result AS A,TEmployee AS B WHERE B.FId = A.EmpId14003 AND A.EmpId14003 != 0
UPDATE A SET A.EmpName14004 = B.FName FROM #Result AS A,TEmployee AS B WHERE B.FId = A.EmpId14004 AND A.EmpId14004 != 0
SELECT FMemId AS MemId, SUM(1) AS Times ,Month
INTO #Data
FROM (
select FMemId,1 AS Times,LEFT(CONVERT(varchar(100), FTime, 112),6) AS Month
from TMemberTransHist
GROUP BY FMemId,FTransId,LEFT(CONVERT(varchar(100), FTime, 112),6)
) AS A
GROUP BY FMemId,Month
DECLARE @BeginDate DATETIME
DECLARE @EndDate DATETIME
SET @BeginDate = @FromMonth + '-01'
SET @EndDate = CONVERT(VARCHAR, DATEADD(MONTH,1,DATEADD(DAY,-1,@ToMonth + '-01')) ,23)
DECLARE @Count INT
SELECT @Count = 0;
DECLARE @StateWhere1 VARCHAR(1000)
DECLARE @StateWhere2 VARCHAR(1000)
DECLARE @StateWhere3 VARCHAR(1000)
DECLARE @StateWhere4 VARCHAR(1000)
SELECT @StateWhere1 = ''
SELECT @StateWhere2 = ''
SELECT @StateWhere3 = ''
SELECT @StateWhere4 = ''
WHILE @EndDate >= @BeginDate
BEGIN
DECLARE @Sql VARCHAR(1000)
DECLARE @ColName VARCHAR(50)
DECLARE @Month VARCHAR(10)
SELECT @Month = LEFT(CONVERT(varchar(100), @EndDate, 112),6);
SELECT @ColName = 'Month' +@Month
SELECT @Sql = ' ALTER TABLE #Result ADD ' + @ColName + ' VARCHAR(10) NOT NULL DEFAULT '''''
EXEC (@Sql )
SELECT @Sql = ' UPDATE A SET A.' + @ColName + ' = B.Times FROM #Result AS A,#Data AS B '
+ ' WHERE A.MemId = B.MemId AND B.Month = ''' + @Month + ''' AND B.Times != 0' ;
print @Sql
EXEC (@Sql )
SELECT @StateWhere1 = @StateWhere1 + ' AND ' + @ColName + ' != 0 '
SELECT @StateWhere2 = @StateWhere2 + ' AND ' + @ColName + ' = 0 '
SELECT @StateWhere3 = @StateWhere3 + ' AND ' + @ColName + ' = 0 '
SELECT @StateWhere4 = @StateWhere4 + ' AND ' + @ColName + ' = 0 '
SET @Count = @Count + 1;
IF @Count = 3
BEGIN
print ''
--3个月连续来的
SELECT @Sql = ' UPDATE #Result SET StateDesc = ''常来客'' WHERE 1=1 '+ @StateWhere1;
EXEC (@Sql )
--3个月连续不来的
SELECT @Sql = ' UPDATE #Result SET StateDesc = ''警报客'' WHERE 1=1 '+ @StateWhere2;
EXEC (@Sql )
END
ELSE IF @Count = 6
BEGIN
SELECT @Sql = ' UPDATE #Result SET StateDesc = ''流失客'' WHERE 1=1 '+ @StateWhere3;
EXEC (@Sql )
END
ELSE IF @Count = 12
BEGIN
SELECT @Sql = ' UPDATE #Result SET StateDesc = ''死客'' WHERE 1=1 '+ @StateWhere4;
EXEC (@Sql )
END
SET @EndDate = DATEADD(MONTH,-1,@EndDate)
END
SET @EndDate = CONVERT(VARCHAR, DATEADD(MONTH,1,DATEADD(DAY,-1,@ToMonth + '-01')) ,23)
SELECT M.*,C.FCompName AS CompName
FROM #Result AS M
INNER JOIN #Company AS C ON C.FCompId = M.CompId
ORDER BY C.FCompNo ASC,M.MemNo ASC
END
GO
EXEC PRptMemberReport20 'CompNo="001,002"FromMonth="2017-01"ToMonth="2017-05"'