我有以下查询,该查询应显示过去3个月的结果:
SELECT COUNT(DISTINCT dbo.UserQuiz.QuizID) AS [Total Number of Quizzes],
dbo.Divisions.DivisionName,
DATENAME(Month, dbo.UserQuiz.DateTimeComplete) AS Month
FROM dbo.UserQuiz
INNER JOIN dbo.Quiz
ON dbo.UserQuiz.QuizID = dbo.Quiz.QuizID
INNER JOIN dbo.employee
ON dbo.UserQuiz.Username = dbo.employee.Username
RIGHT OUTER JOIN dbo.Divisions
ON dbo.employee.DivisionCode = dbo.Divisions.SapCode
GROUP BY dbo.Divisions.DivisionName,
DATENAME(Month, dbo.UserQuiz.DateTimeComplete)
HAVING (DATENAME(Month, GETDATE()) -
DATENAME(Month, dbo.UserQuiz.DateTimeComplete) > 3
此查询现在出现的问题是当我尝试在SQLServer Management Studio 2008 R2中执行它时,出现以下错误,我不知道原因:
Msg 102,Level 15,State 1,Line 5'3'附近的语法不正确。
答案 0 :(得分:2)
在3之后需要一个末端括号。
SELECT
COUNT(DISTINCT dbo.UserQuiz.QuizID) AS [Total Number of Quizzes],
dbo.Divisions.DivisionName,
DATENAME(Month, dbo.UserQuiz.DateTimeComplete) AS Month
FROM dbo.UserQuiz
INNER JOIN dbo.Quiz
ON dbo.UserQuiz.QuizID = dbo.Quiz.QuizID
INNER JOIN dbo.employee
ON dbo.UserQuiz.Username = dbo.employee.Username
RIGHT OUTER JOIN dbo.Divisions
ON dbo.employee.DivisionCode = dbo.Divisions.SapCode
GROUP BY dbo.Divisions.DivisionName,
DATENAME(Month, dbo.UserQuiz.DateTimeComplete)
HAVING (
DATENAME(Month, GETDATE())
- DATENAME(Month, dbo.UserQuiz.DateTimeComplete) > 3
) -- you were missing this parenthesis
答案 1 :(得分:0)
我相信你在某个地方错过了一个')'。
HAVING (DATENAME(Month, GETDATE()) - DATENAME(Month, dbo.UserQuiz.DateTimeComplete) > 3