查询在sql server 2012中有效,但在server 2008中无效

时间:2014-09-18 19:15:33

标签: sql-server sql-server-2008 sql-server-2008-r2 sql-server-2012

我创建了以下查询,它在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

1 个答案:

答案 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/