我在这个CASE声明中遇到了一些麻烦...... 它说我在>附近收到错误这似乎指向第3行,其中检查> 15.但是我没有看到这个查询有什么问题。
我尝试在谷歌和这里搜索,并没有看到任何人有这个确切的问题,希望你能指出来。
SELECT
CASE
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 15 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 30 THEN '15+'
ELSE DATEDIFF(day, C1.RecvdDate, GETDATE()) > 30 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 60 THEN '30+'
ELSE DATEDIFF(day, C1.RecvdDate, GETDATE()) > 60 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 90 THEN '60+'
ELSE DATEDIFF(day, C1.RecvdDate, GETDATE()) > 90 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 120 THEN '90+'
ELSE '120+'
END AS 'Days',
C1.CallID AS 'Ticket#', A1.WhoAcknow AS 'Acknowledged By', C1.RecvdDate AS 'Received On', C1.Category, COUNT(*) As Assignments
FROM [HEAT].[heat].[CallLog] C1
LEFT JOIN [HEAT].[heat].[Asgnmnt] A1 ON C1.CallID = A1.CallID
WHERE DATEDIFF(day, C1.RecvdDate, GETDATE()) > 15 AND C1.CallStatus = 'Open' AND C1.Category <> 'welfare' AND C1.CustType <> 'IFS' AND
A1.Resolution = '' AND (A1.GroupName = 'Help Desk' AND A1.Assignee = 'EITS PIV Badge')
GROUP BY C1.CallID, A1.WhoAcknow, C1.RecvdDate, C1.Category
ORDER BY C1.RecvdDate ASC
提前致谢!
答案 0 :(得分:5)
您的问题在于CASE
表达式。使用多个条件时,请使用WHEN
代替ELSE
。 ELSE
只能使用一次。
SELECT
CASE
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 15 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 30 THEN '15+'
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 30 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 60 THEN '30+'
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 60 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 90 THEN '60+'
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 90 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 120 THEN '90+'
ELSE '120+'
END AS 'Days'...
答案 1 :(得分:2)
你的语法错了;你不能使用多个ELSE子句 - 它应该是多个WHENs:
SELECT
CASE
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 15 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 30 THEN '15+'
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 30 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 60 THEN '30+'
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 60 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 90 THEN '60+'
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 90 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 120 THEN '90+'
ELSE '120+'
END AS 'Days'
有关更多信息和示例,请参阅the documentation。
答案 2 :(得分:2)
我相信这是你对CASE语句的格式化,我把你的查询是SSMS并以这种方式修复它,并通过验证:
每个CASE语句只能使用1个ELSE子句
SELECT
CASE
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 15 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 30 THEN '15+'
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 30 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 60 THEN '30+'
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 60 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 90 THEN '60+'
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 90 AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 120 THEN '90+'
ELSE '120+'
END AS 'Days',
C1.CallID AS 'Ticket#', A1.WhoAcknow AS 'Acknowledged By', C1.RecvdDate AS 'Received On', C1.Category, COUNT(*) As Assignments
FROM [HEAT].[heat].[CallLog] C1
LEFT JOIN [HEAT].[heat].[Asgnmnt] A1 ON C1.CallID = A1.CallID
WHERE DATEDIFF(day, C1.RecvdDate, GETDATE()) > 15 AND C1.CallStatus = 'Open' AND C1.Category <> 'welfare' AND C1.CustType <> 'IFS' AND
A1.Resolution = '' AND (A1.GroupName = 'Help Desk' AND A1.Assignee = 'EITS PIV Badge')
GROUP BY C1.CallID, A1.WhoAcknow, C1.RecvdDate, C1.Category
ORDER BY C1.RecvdDate ASC