我正在构建一个现金返还报告的问题。这是我的存储过程的代码和我想要的。
--SP CODE
CREATE PROCEDURE [dbo].[CashbackPromo]
@startDate DateTime,
@endDate DateTime
AS
create table #CashbackInfo
(
UserID int,
PreviousBal money,
DepositAmount money,
NewBalance money,
LostMoney money
);
--DECLARE @startDate DateTime ='2014-06-07 00:00:00'
--DECLARE @endDate DateTime ='2014-06-08 00:00:00';
WITH Deposits
AS
(
SELECT DISTINCT userID FROM CurrencyTransactions where timestamp between @startDate and @endDate and transactionType=1
),
Withdrawl
AS
(
SELECT DISTINCT userID FROM Withdrawals where timestampRequested between @startDate and @endDate
),
WithoutWithdrawals
AS
(
SELECT b.userID,w.userID as uID from Deposits as b LEFT JOIN Withdrawl as w on w.userID=b.userID
WHERE ISNULL(w.userID,0) = 0
),
DepositDetails
AS
(
SELECT * FROM CurrencyTransactions where userID in(SELECT userID from WithoutWithdrawals) and transactionType=1 and timestamp between @startDate and @endDate
)
--DELETE FROM #CashBackInfo
INSERT INTO #CashbackInfo(UserID,PreviousBal,DepositAmount,NewBalance,LostMoney)
(SELECT UserID,(newBalance-deltaRupees),deltaRupees,newBalance,0 FROM DepositDetails);
SELECT * FROM #CashbackInfo Order By UserID