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/会员项目统计.sql

143 lines
3.2 KiB

use JiNing_LingXiu
SELECT * FROM TBasicType WHERE FKey LIKE 'ItemType'
drop table #Result
CREATE TABLE #Result
(
FMemId BIGINT,
FMemNO VARCHAR(100),
FMobile VARCHAR(100),
FCardTypeNames NVARCHAR(1000),
FAmount DECIMAL(18,6),
FSmallQuantity DECIMAL(18,6),
FBigQuantity DECIMAL(18,6),
FSmallAmount DECIMAL(18,6),
FBigAmount DECIMAL(18,6),
FBizEmpNames NVARCHAR(1000)
)
--卡类别和负责员工
SELECT * FROM TM
SELECT * INTO #Hist FROM TMemberTransHist
WHERE FTime BETWEEN '2020-08-01' AND '2021-07-31'
--全部
SELECT FMemId,SUM(FQuantity) AS FQuantity,SUM(FAmount) AS FAmount INTO #All
FROM #Hist
GROUP BY FMemId
--小项目
SELECT FMemId,SUM(FQuantity) AS FQuantity,SUM(FAmount) AS FAmount INTO #Small
FROM #Hist
WHERE FCOntentId IN(
SELECT FId FROM TItem WHERE FTypeId IN(SELECT FId FROM TBasicType WHERE FKey = 'ItemType' AND FName like '%剪发%')
)
GROUP BY FMemId
--大项目
SELECT FMemId,SUM(FQuantity) AS FQuantity,SUM(FAmount) AS FAmount INTO #Big
FROM #Hist
WHERE FCOntentId IN(
SELECT FId FROM TItem WHERE FTypeId IN(SELECT FId FROM TBasicType WHERE FKey = 'ItemType' AND FName like '%烫%' OR FName lIKE '%染%')
)
GROUP BY FMemId
INSERT #Result(FMemId,FMemNO,FMobile)
SELECT FMemId,FMemNO,FMobilePHone
FROM TMember
WHERE FMemId IN(SELECT FMemId FROM #Hist)
AND FDeleted = 0
UPDATE A
SET A.FSmallQuantity = B.FQuantity,
A.FSmallAmount = B.FAmount
FROM #Result AS A,#Small AS B
WHERE A.FMemId = B.FMemId
UPDATE A
SET A.FBigQuantity = B.FQuantity,
A.FBigAmount = B.FAmount
FROM #Result AS A,#Big AS B
WHERE A.FMemId = B.FMemId
UPDATE A
SET A.FAmount = B.FAmount
FROM #Result AS A,#all AS B
WHERE A.FMemId = B.FMemId
--卡类别
SELECT A.FMemId,B.FName AS FCardTypeName
INTO #CardType
FROM TMemberCard AS A,TMemberCardType AS B
WHERE A.FTypeId = B.FId
AND A.FMemId IN(SELECT FMemId FROM #Result)
GROUP BY A.FMemId,B.FName
DROP TABLE #CardType
UPDATE A
SET A.FCardTypeNames = B.FCardTypeName
FROM #Result AS A,(
SELECT Test.FMemId ,
FCardTypeName = ( STUFF(( SELECT ',' + FCardTypeName
FROM #Card
WHERE FMemId = Test.FMemId
FOR
XML PATH('')
), 1, 1, '') )
FROM #Card AS Test
GROUP BY FMemId) AS B
WHERE A.FMemId = B.FMemId
--员工
SELECT A.FMemId,B.FName AS FBizEmpName
INto #Emp
FROM TMemberCard AS A,TEmployee AS B
WHERE A.FBizEmpId = B.FId
AND A.FMemId IN(SELECT FMemId FROM #Result)
AND A.FBizEmpId != 0
GROUP BY A.FMemId,B.FName
UPDATE A
SET A.FBizEmpNames = B.FBizEmpName
FROM #Result AS A,(
SELECT Test.FMemId ,
FBizEmpName = ( STUFF(( SELECT ',' + FBizEmpName
FROM #emp
WHERE FMemId = Test.FMemId
FOR
XML PATH('')
), 1, 1, '') )
FROM #Card AS Test
GROUP BY FMemId) AS B
WHERE A.FMemId = B.FMemId
SELECT FMemNo AS ,FMobile AS ,FCardTypeNames as ,FAMount as ,ISNULL(FSmallQuantity ,0)AS ,
/*ISNULL(FSmallAmount ,0)AS 剪发金额, */ISNULL(FBigQuantity ,0)AS ,ISNULL(FBigAmount,0) AS ,ISNULL(fbizeMPnames ,'')as
FROM #Result