0

这是我的代码,改编自这些论坛上其他人的回答。但是它告诉我“”“消息 102,第 15 级,状态 1,第 4 行 '(' 附近的语法不正确。”

我是 SQL 新手,我找不到任何帮助修复它的错误,将不胜感激。

“cteMain:我不知道那是什么或它引用了什么,VieClaimTrans 是 TrnClaimTrans 的一个视图,我正在使用的所有信息都存储在其中。”

WITH cteMain AS

(SELECT sn.ClientID , sn.FileNo, sn.ClaimNo, sn.TransEstimate, sn.TransType, ROW_NUMBER() OVER (PARTITION BY sn.TransType, sn.ClientID, sn.FileNo, sn.ClaimNo ORDER BY sn.ClientID, sn.FileNo, sn.ClaimNo)
FROM VieClaimTrans AS sn)

SELECT m.ClientID, m.FileNo, m.ClaimNo, m.TransType, 
   ISNULL(sLag.TransEstimate, 0) AS prevEstimate,
  (m.TransEstimate - ISNULL(sLag.TransEstimate, 0)) AS movement
FROM TrnClaimTrans AS m

LEFT OUTER JOIN cteMain AS sLag
ON sLag.ClientID = m.ClientID
AND sLag.FileNo = m.FileNo
AND sLag.ClaimNo = m.ClaimNo
AND sLag.sn = m.sn-1

4

1 回答 1

0

您在 ROW_NUMBER() 前面缺少逗号

;WITH cteMain AS

(SELECT sn.ClientID , sn.FileNo, sn.ClaimNo, sn.TransEstimate, 

--你应该在row_number()之前加上你的逗号,加油

sn.TransType, ROW_NUMBER() OVER (PARTITION BY sn.TransType, sn.ClientID, 
sn.FileNo,sn.ClaimNo ORDER BY sn.ClientID, sn.FileNo, sn.ClaimNo) '[ROWNAME]'

-- 在 row_number() 之后添加你的变量名

FROM VieClaimTrans AS sn)

SELECT m.ClientID, m.FileNo, m.ClaimNo, m.TransType, 
ISNULL(sLag.TransEstimate, 0) AS prevEstimate,
(m.TransEstimate - ISNULL(sLag.TransEstimate, 0)) AS movement
FROM TrnClaimTrans AS m

LEFT OUTER JOIN cteMain AS sLag
ON sLag.ClientID = m.ClientID
AND sLag.FileNo = m.FileNo
AND sLag.ClaimNo = m.ClaimNo
AND sLag.sn = m.sn-1
于 2013-10-04T08:08:00.803 回答