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

相關問題