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/temp/柯林转老账户.sql

125 lines
3.5 KiB

1 year ago
use wuhan_kelin
SELECT * FROM TConsumeBill ORDER BY FBillTime desc
DECLARE List FOR SELECT
SELECT A.FMemId,A.FCardId,A.FAcctId,A.FBalance,B.FTypeId AS FCardTypeId,B.FCompId
INTO #Acct
FROM TMemberCardAccount AS A,TMemberCard AS B
WHERE FBalance > 0
AND A.FCardId = B.FCardId
AND B.FCreateDate < '2021-10-01'
AND A.FAcctId = 3
AND B.FDeleted = 0
drop table #newacct
SELECT A.FMemId,A.FCardId,A.FCardTypeId,A.FCompId,A.FAcctId,A.FOldBalance ,B.FNewOutBalance,3 AS FOutAcctId ,
4877729984662584702 AS FInAcctId,
A.FOldBalance - ISNULL(B.FNewOutBalance,0) AS FChangeBalance ,--Ӧ<EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
dbo.FNNewId() AS FTransId
INTO #NewAcct
FROM (
--<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>10<EFBFBD><EFBFBD>1<EFBFBD>ŵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SELECT A.FMemId,A.FCardId,B.FCardTypeId,B.FCompId,A.FAcctId,SUM(FInAmount) - SUM(FOutAmount) AS FOldBalance
FROM TMemberCardAcctHist AS A,#Acct AS B
WHERE A.FCardId = B.FCardId
AND A.FAcctId = B.FAcctId
AND A.FTime < '2021-10-01'
GROUP BY A.FMemId,A.FCardId,A.FAcctId ,B.FCompId,B.FCardTypeId
) AS A
LEFT JOIN
(
--<EFBFBD><EFBFBD>ȡ10<EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѵĽ<EFBFBD><EFBFBD><EFBFBD>
SELECT A.FCardId,A.FAcctId, SUM(FOutAmount) AS FNewOutBalance
FROM TMemberCardAcctHist AS A,#Acct AS B
WHERE A.FCardId = B.FCardId
AND A.FAcctId = B.FAcctId
AND A.FTime > '2021-10-01'
GROUP BY A.FCardId,A.FAcctId
) AS B
ON A.FCardId = B.FCardId
AND A.FAcctId = B.FAcctId
WHERE A.FOldBalance > ISNULL(B.FNewOutBalance,0)
BEGIN TRAN
INSERT TMemberCardAcctHist(FId,FMemId,FCardId,FAcctId,FTime,FCardTypeId,FCompId,FBillType,FBizType,FTransId,FTransNo,FInAmount,FOutAmount,FBalance,FArrear,FRecordTime,FMemo)
select dbo.FNNewId(),A.FMemId,A.FCardId,A.FInAcctId,getdate(),A.FCardTypeId,A.FCompId,'ZZ','Transfer.In',A.FTransId,'',A.FChangeBalance,0,0,0,getdate(),'2021-10-01֮ǰ<EFBFBD>Ĵ<EFBFBD>ֵ<EFBFBD>˻<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>ϴ<EFBFBD>ֵ<EFBFBD>˻<EFBFBD>'
FROM #NewAcct AS A
INSERT TMemberCardAcctHist(FId,FMemId,FCardId,FAcctId,FTime,FCardTypeId,FCompId,FBillType,FBizType,FTransId,FTransNo,FInAmount,FOutAmount,FBalance,FArrear,FRecordTime,FMemo)
select dbo.FNNewId(),A.FMemId,A.FCardId,A.FOutAcctId,getdate(),A.FCardTypeId,A.FCompId,'ZZ','Transfer.Out',A.FTransId,'',0,A.FChangeBalance,0,0,getdate(),'2021-10-01֮ǰ<EFBFBD>Ĵ<EFBFBD>ֵ<EFBFBD>˻<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>ϴ<EFBFBD>ֵ<EFBFBD>˻<EFBFBD>'
FROM #NewAcct AS A
INSERT TMemberCardAccount(FId,FMemId,FCardId,FAcctId,FDeposit,FBalance,FArrear,FCreateDate,FMemo)
SELECT dbo.FNNewId(),A.FMemId,A.FCardID,A.FInAcctId,A.FChangeBalance,A.FChangeBalance,0,GETDATE(),''
from #NewAcct AS A
sp_rename '#NewAcct','TChangeAccount_2021_10_01'
select * into TChangeAccount_2021_10_01
FROM #NewAcct
UPDATE A
SET A.FBalance = A.FBalance - B.FChangeBalance
FROM TMemberCardAccount AS A,#NewAcct AS B
WHERE A.FCardId = B.FCardId
AND A.FAcctId = B.FAcctId
ROLLBACK TRAN
SELECT * FROM
(
SELECT FCardId,FAcctId,FBalance
FROM TMemberCardAccount
) AS T1,
(
SELECT FCardId,FAcctId, SUM(FInAmount) - SUM(FOutAmount) AS FBalance
FROM TMemberCardAcctHist
GROUP BY FCardId,FAcctId
) AS T2
WHERE T1.FCardId = T2.FCardId
AND T1.FAcctId = T2.FAcctId
AND T2.FBalance != T1.FBalance
SELECT
'EXEC
dbo.PRecalcCardAccHistBalance ' + CAST(FCardId AS VARCHAR) FROM TMemberCard
WHERE FCardId IN(SELECT FCardId FROM #Acct)
SELECT * FROM TPayAccount
SELECT * FROM #Acct AS A,#hist AS B
WHERE A.FCardId = B.FCardId
AND A.FAcctId = B.FAcctId
AND B.FOldBalance != A.FBalance
SELECT 'EXEC PDeleteMemberSaleBill '+ CAST(FBilLid AS VARCHAR) FROM TMemberSaleBill
WHERE FBilLTime > '2022-01-01'
AND FBilLTime <= '2022-01-02'
ORDER BY FBilLTime desc
SELECT C.FNo,C.FAbbr,A.FBillNo FROM TMemberSaleBill AS A,TCompany AS C
WHERE FBilLTime > '2022-01-01'
AND FBilLTime <= '2022-01-02'
AND C.FId = A.FCompId
ORDER BY C.FNo, FBilLTime desc