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

126 lines
2.9 KiB

1 year ago
use zibo_xianggetianyuan
SELECT *INTO #Bill FROM TMemberSaleBill WHERE FBizType = 'Exchange'
AND FState = 1
SELECT * FROM TMemberSaleExchange WHERE FChangemode = 2
SELECT * FROM TMemberSaleCourse
SELECT * FROM TMemberSaleExchangeTarget
DROP TABLE #R
CREATE TABLE #R
(
FBillId BIGINT,
FMemNo VARCHAR(50),
FMemName VARCHAR(50),
FMobile VARCHAR(50),
FBillNo VARCHAR(50),
FType VARCHAR(30),
FCourseName VARCHAR(100),
FPurTimes DECIMAL(18,6),
FPurAmount DECIMAL(18,6),
FFreeTimes DECIMAL(18,6),
FFreeAmount DECIMAL(18,6),
FIndex INT,
FId BIGINT
)
ALTER TABLE #R ADD FId BIGINT
ALTER TABLE #R ADD FIndex INT
DELETE #R
UPDATE #R SET FId = dbo.FNNewId()
SELECT * FROM #Bill
INSERT #R(FMemNO,FBillId,FBillNO,FType,FCourseName,FPurTimes,FPurAmount,FFReeTimes,FFReeAmount)
SELECT T.FMemNo ,T.FBIllId,T.FBillNo,'ԭ<EFBFBD>Ƴ<EFBFBD>' AS Type,B.FNo + '-' + B.FName AS FCOurseName,
A.FPurTimes,A.FPurAmount,A.FFreeTimes ,A.FFreeAMount
FROM TMemberSaleExchangeSource
AS A,TCOurse AS B,#Bill AS T
WHERE B.FId = A.FCourseId
AND A.FBillId = T.FBillId
UNION ALL
SELECT T.FMemNO,T.FBillId,T.FBillNo,'<EFBFBD><EFBFBD><EFBFBD>Ƴ<EFBFBD>' AS Type,B.FNo + '-' + B.FName AS FCourseName,
A.FPurTimes,A.FPurAmount,A.FFreeTimes ,A.FFreeAMount
FROM TMemberSaleCourse
AS A,TCOurse AS B,#Bill AS T
WHERE B.FId = A.FCourseId
AND A.FBillId = T.FBillId
ORDER BY FMemNo,FBillNo
UPDATE A
SET A.FMemName = B.FName,
A.FMObile = B.FMobilePhone
FROM #R AS A,TMember AS B
WHERE A.FMemNo = B.FMemNo
UPDATE #R SET FType = FType + 'ת<EFBFBD><EFBFBD>ֵ'
WHERE FBillId IN(SELECT FBillId FROM TMemberSaleExchange WHERE FChangemode = 2)
UPDATE #R SET FType = FType + '<EFBFBD><EFBFBD><EFBFBD>Ƴ<EFBFBD>'
WHERE FBillId IN(SELECT FBillId FROM TMemberSaleExchange WHERE FChangemode = 3)
DECLARE @MemNo VARCHAR(50)
DECLARE @LastMemNo VARCHAR(50)
DECLARE @Id BIGINT
DECLARE @Index INT
SELECT @Index = 1
SELECT @LastMemNo = ''
DECLARE List CURSOR FOR SELECT FId,FMemNo FROM #R ORDER BY FMemNo,FBillNo,FType
OPEN List
FETCH List INTO @Id,@MemNo
WHILE @@FETCH_STATUS = 0
BEGIN
IF @LastMemNo != @MemNo
BEGIN
INSERT #R(FId,FIndex,FMemNO,FBillNO,FType,FCourseName,FPurTimes,FPurAmount,FFReeTimes,FFReeAmount)
SELECT 0,@Index,'','','','',0,0,0,0
SELECT @Index = @Index + 1
END
-- ELSE
BEGIN
UPDATE #R SET FIndex = @Index WHERE FId = @Id
END
PRINT @Id
PRINT @MemNo
PRINT @LastMemNo
SELECT @LastMemNo = @MemNo
SET @Index = @Index + 1
FETCH LIST Into @Id,@MemNo
END
CLOSE List
DEALLOCATE List
DELETE #R WHERE FMemNo = ''
SELECT FMemNo,ISNULL(FMemName,''),ISNULL(FMobile,''),FBillNo,FType,FCourseName,FPurTimes,FPurAmount,FFreeTimes,FFReeAmount FROM #R
ORDER BY FIndex
SELECT * FROM TMemberSaleBill WHERE FBillNo = '0041604290008'
SELECT * FROM TMemberSaleExchangeSource WHERE FBillId = 5205975583635915165
SELECT * FROM TMemberSaleExchange WHERE FBillId = 5205975583635915165
SELECT* FROM #R ORDER BY FMemNo,FBillNo,FType