子查询:帮助纠正语法错误

时间:2014-01-06 18:08:20

标签: subquery

当我尝试运行以下子查询时,我收到以下2个错误: 消息156,级别15,状态1,行11 - 关键字“SELECT”附近的语法不正确。 消息102,级别15,状态1,行13 - ')'附近的语法不正确。

SELECT
RIGHT(A.Name,LEN(Name)-5) AS ASSOCIATE
,B.WeekEnding
,COUNT(B.ID)AS Policies
,SUM (B.Amount) AS Premium


FROM
dbo.Users A
INNER JOIN (SELECT*,
SELECT DATEADD(DAY,(12-DATEPART(WEEKDAY,CreateDate)%7)-6,CreateDate)
WeekEnding
FROM dbo.Policy) B
ON A.User = B.Agent 

WHERE
B.CreateDate >= CONVERT (DATE,GETDATE()))


GROUP BY A.Name, B.WeekEnding
ORDER BY Policies DESC, Premium DESC

1 个答案:

答案 0 :(得分:0)

子查询中有一个额外的SELECT。它应该是:

(SELECT *,
        DATEADD(DAY,(12-DATEPART(WEEKDAY,CreateDate)%7)-6,CreateDate) WeekEnding
 FROM dbo.Policy)

你在这里有一个额外的括号:

WHERE
B.CreateDate >= CONVERT (DATE,GETDATE()))
                                        ^