关键字“ON”附近的语法错误

时间:2014-01-17 15:39:51

标签: sql sql-server syntax-error

  

第15行,第15行,第1行,第12行:
  关键字“ON”附近的语法不正确。

查询:

SELECT 
    A.Name, Count(B.ID) AS Policies, Sum(B.Fee) AS Premium
FROM 
    Table1 A
INNER JOIN 
    (SELECT *, DATEADD(DAY, (12-DATEPART(WEEKDAY,B.CreateDate) % 7 - 6, B.CreateDate) WeekEnding
FROM Table2 B ON A.ID = B.EmpID

WHERE 
    WeekEnding >= CONVERT(DATE, GETDATE())
GROUP BY 
    A.Name
ORDER BY 
    Policies DESC, Premium DESC

2 个答案:

答案 0 :(得分:2)

此处有八个左括号,只有结束。 SQL需要等量的每种类型的括号。 下次使用类似Notepad ++之类的东西来检查代码 - 用它来查找语法错误要容易得多。

答案 1 :(得分:0)

这适合你吗?

SELECT 
  A.Name, Count(B.ID) AS Policies, Sum(B.Fee) AS Premium
FROM 
    Table1 A
INNER JOIN 
    (SELECT *, DATEADD(DAY, (12-DATEPART(WEEKDAY,B.CreateDate) % 7 - 6, B.CreateDate) WeekEnding)
FROM Table2 B ON A.ID = B.EmpID

WHERE 
    WeekEnding >= CONVERT(DATE, GETDATE()))
GROUP BY 
    A.Name
ORDER BY 
    Policies DESC, Premium DESC