Sql server sql server(查找两行中两列之间的差异)
我对正在生成的现金返还报告有问题。这是我的存储过程的代码和我想要的Sql server sql server(查找两行中两列之间的差异),sql-server,stored-procedures,Sql Server,Stored Procedures,我对正在生成的现金返还报告有问题。这是我的存储过程的代码和我想要的 --SP CODE CREATE PROCEDURE [dbo].[CashbackPromo] @startDate DateTime, @endDate DateTime AS create table #CashbackInfo ( UserID int, PreviousBal money, DepositAmount money,
--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
感谢@blueFeet编辑我是这个网站的新手,不知道如何设置代码的格式在此上下文中,
previous
和next
是什么意思?你需要一个独特的<代码>命令来定义订单。我用USER ID排序,这意味着如果用户有一个以上的条目,那么我们会考虑他,否则我们将被取消资格,并且我们可以通过USERID来划分出具有RunNoMeNe()的单个条目用户。<代码>选择NealStale-引线(PrimiiSbAbl,1, 0)。(按用户ID按顺序按???划分?将提供两行之间的差异,但由于即使您不知道哪一行是第一行,我也无法提供更多帮助。您应该将时间戳数据添加到查询中以帮助排序,然后您将知道哪一行是第一行存款。