1

我有一份报告要检查重复的“recs”,但有点丢失并出现错误。看一看!

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'附近的语法不正确。” 哪里语法不正确??

提前致谢!

4

2 回答 2

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

加入什么条件?

于 2012-04-13T11:08:32.627 回答
1
WHERE temp.FirstVoucher != temp.LastVoucher

<> 是这里的标准。不确定使用 != 是否会对性能产生任何轻微影响……但 MSFT 更喜欢 <>。

于 2012-04-13T11:02:27.917 回答