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

144 lines
3.2 KiB

1 year ago
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)
)
--<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͸<EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD>
SELECT * FROM TM
SELECT * INTO #Hist FROM TMemberTransHist
WHERE FTime BETWEEN '2020-08-01' AND '2021-07-31'
--ȫ<EFBFBD><EFBFBD>
SELECT FMemId,SUM(FQuantity) AS FQuantity,SUM(FAmount) AS FAmount INTO #All
FROM #Hist
GROUP BY FMemId
--С<EFBFBD><EFBFBD>Ŀ
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 '%<EFBFBD><EFBFBD><EFBFBD><EFBFBD>%')
)
GROUP BY FMemId
--<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ
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 '%<EFBFBD><EFBFBD>%' 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
--<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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
--Ա<EFBFBD><EFBFBD>
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 <EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,FMobile AS <EFBFBD>ֻ<EFBFBD>,FCardTypeNames as <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,FAMount as <EFBFBD><EFBFBD><EFBFBD>ѽ<EFBFBD><EFBFBD><EFBFBD>,ISNULL(FSmallQuantity ,0)AS <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,
/*ISNULL(FSmallAmount ,0)AS <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, */ISNULL(FBigQuantity ,0)AS <EFBFBD><EFBFBD>Ⱦ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ISNULL(FBigAmount,0) AS <EFBFBD><EFBFBD>Ⱦ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ISNULL(fbizeMPnames ,'')as <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD>
FROM #Result