2012-04-13 102 views
1

我有一個報告來檢查重複的「recs」,但有一點丟失,並得到一個錯誤。看一看!SQL報告錯誤在哪裏

Declare @StartDate as Datetime 
Declare @EndDate as Datetime 

set @StartDate = GetDate()-14 
set @EndDate = GetDate() 


SELECT * 
FROM (SELECT dupl.machID, dupl.Location, dupl.place, dupl.recNumber, 
MIN(realdupl.rcTransDate) 

AS Firstrec, MAX(realdupl.rcTransDate) AS Lastrec 

FROM  
    (SELECT rc.rcmachID AS machID, ck.ckLocation AS Location,  

    ck.ckLocationplace AS place, rc.rcrecNum AS recNumber, COUNT(*) as 
    NoOfrecs 
FROM 

    (SELECT rcmachID, rcrecNum, rcTransDate, rcturnaroundIndicator FROM  
    ProBatchHostDbSec.dbo.vatrecs (NOLOCK) 
      ) 
     AS rc 
      INNER JOIN machConfig AS ck WITH (NOLOCK) 
      ON rc.rcmachID = ck.ckmachID 
      WHERE rc.rcturnaroundIndicator = 0 
      AND ck.ckProductionmach = 'Y' 
      AND ck.ckLocationplace = 'IN' 
      GROUP BY rc.rcmachID, ck.ckLocation, 
            ck.ckLocationplace, rc.rcrecNum 
      HAVING COUNT(*) > 1 
      ) AS dupl 
    INNER JOIN 
    (SELECT rcmachID, rcrecNum, rcTransDate, rcturnaroundIndicator FROM 
      ProBatchHostDbSec.dbo.vatrecs 

     AND dupl.recNumber = realdupl.rcrecNum 
    GROUP BY dupl.machID, dupl.Location, dupl.place, dupl.recNumber 
    ) AS temp 

     WHERE temp.Firstrec != temp.Lastrec 
     AND temp.Lastrec BETWEEN @StartDate AND @EndDate 
      ORDER BY temp.machID, temp.Lastrecdbo.vatrecs --WITH (NOLOCK) 
    GROUP BY dupl.machID, dupl.Location, dupl.place, dupl.recNumber 
    ) AS temp 

當我運行這個時,我得到一個錯誤「關鍵字'WHERE'附近的語法不正確。 Wheres錯誤的語法?

在此先感謝!

回答

1
WHERE temp.FirstVoucher != temp.LastVoucher 

<>是這裏的標準。不知道使用!=對性能有什麼小的影響,但是MSFT更喜歡<>。

+0

該部分工作得很好 – 2012-04-13 11:03:45

+0

!=是一個有效的運算符。 – 2012-04-13 11:05:51

4
INNER JOIN 
(SELECT vsTermID, vsVoucherNum, vsTransDate, vsReversalIndicator FROM 
     ProBatchHostDbSec.dbo.vatVouchers 

    AND dupl.VoucherNumber = realdupl.vsVoucherNum 
GROUP BY dupl.TerminalID, dupl.Location, dupl.Country, dupl.VoucherNumber 
) AS temp 

    WHERE temp.FirstVoucher != temp.LastVoucher 

加入什麼條件?

+0

是的..會看看並回復。謝謝。 – Axle 2012-04-13 12:48:17