0

我在下面创建的查询,它在sql server 2012中工作良好,但在sql server 2008或2008R2中不起作用。请帮我解决这个问题。查询在sql server 2012中可用,但在服务器2008中不可用

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 [email protected] 
    ) AS G 
+3

的代码在这种格式下是相当难读的,但是我把这笔钱放在这个原因上:'1000之前的行和当前行之间的行'。如果我不是mi这个语法最近才在SQ Server中引入。 – SchmitzIT 2014-09-18 19:17:15

+0

是的这种语法是在sql server 2012中,这是它在2008年不工作的原因。请建议,以便它也可以在2008年。 – 2014-09-18 19:18:34

回答

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/

+0

谢谢,但有没有更改此查询,以便它可以在2008年工作。请帮助我一样。 – 2014-09-18 19:22:12

+0

@VidyaVikasMishra它需要一个重要的重写。这是Sql Server 2012的一个新功能,无需您重新考虑如何编写查询。 – 2014-09-18 19:55:51

相关问题