我创建了以下查询,它在sql server 2012中运行良好,但在sql server 2008或2008R2中无效。请帮我解决这个问题。
Select Batchno, BatchId, Transactiontype, LoanAccountNumber, TransactionId
, StaticDetailId, Name, Comment, BatchTotal
, case when Amount<0 then Amount*-1
Else Amount End as Amount
, ((BatchTotal) - (SUM(case when Amount<0 then Amount*-1 Else Amount End)
OVER(order BY Batchno ROWS BETWEEN 1000 Preceding and current row))) AS BatchBal
From (SELECT row_number() over (order by BO.BatchID) as [BatchNo]
, BO.BatchID, sd.TransactionType, tra.LoanAccountNumber, tra.TransactionID
, tra.StaticDetailID,tra.Name,tra.Comment, BO.BatchTotal,tra.Amount
FROM BackOfficeBatchTransactionDetails tra
join BackOfficeBatchDetails BO
on BO.BatchID=tra.BatchID and tra.isactive=1 and tra.isdeleted=0
join StaticDetails sd on sd.StaticDetailID=tra.StaticDetailID and sd.isactive=1
where BO.isactive=1 and BO.isdeleted=0 AND BO.BatchId=@BatchID
) AS G
答案 0 :(得分:3)
查询失败的原因是声明的这一点:
ROWS BETWEEN 1000 Preceding and current row
ROWS
(和RANGE
关键字仅在SQL Server 2012中引入:
http://www.pawlowski.cz/2012/06/ms-sql-2012-window-functions-introduction/