我建立了一个矩阵,将多个部门的记录汇总到部门组中。我正在尝试建立详细报告。我在尝试将作为参数传递给细分的部门组名称转换为组成该组的各个部门编号时遇到麻烦。
使用带有MS Report Builder 3.0的SQL Server 2012。
尝试在WHERE子句中使用CASE语句返回与参数中传递的部门组名称关联的部门号。这适用于包含单个部门编号的部门组,但是只要该部门组包含多个部门,就会出错。
此代码适用于单部门部门组,但是当case语句包含多部门组时失败。
NPD.ACT_DEPTARTMENT IN (CASE
WHEN @DEPT = 'Business Group' THEN ('40', '43', '45', '46', '47', '83', '84', '85', '86', '87', '89', '90')
WHEN @DEPT = 'Chemistry Group' THEN ('06', '07')
WHEN @DEPT = 'Emergency Prep' THEN ('33')
WHEN @DEPT = 'Engineering Group' THEN ('60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72')
WHEN @DEPT = 'Maintain Group' THEN ('21', '22', '23', '24', '25', '26', '76', '78')
WHEN @DEPT = 'Management Group' THEN ('36', '48', '80', '81', '88', '96')
WHEN @DEPT = 'Operations Group' THEN ('11', '12', '13')
WHEN @DEPT = 'Projects Group' THEN ('54', '73', '79')
WHEN @DEPT = 'Security' THEN ('34')
WHEN @DEPT = 'Supply Group' THEN ('50', '98')
WHEN @DEPT = 'Training Group' THEN ('82', '91', '92', '93', '94', '95')
END)
最初尝试了此代码,但无法正常运行。 Googling表示我正在尝试让CASE执行代码,而不仅仅是返回"IN"
表达式的一部分。
AND CASE WHEN @DEPT = 'Business Group' THEN NPD_ACT.DEPARTMENT IN ('40', '43', '45', '46', '47', '83', '84', '85', '86', '87', '89', '90')
WHEN @DEPT = 'Chemistry Group' THEN NPD_ACT.DEPARTMENT IN ('06', '07')
WHEN @DEPT = 'Emergency Prep' THEN NPD_ACT.DEPARTMENT IN ('33')
WHEN @DEPT = 'Engineering Group' THEN NPD_ACT.DEPARTMENT IN ('60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72')
WHEN @DEPT = 'Maintain Group' THEN NPD_ACT.DEPARTMENT IN ('21', '22', '23', '24', '25', '26', '76', '78')
WHEN @DEPT = 'Management Group' THEN NPD_ACT.DEPARTMENT IN ('36', '48', '80', '81', '88', '96')
WHEN @DEPT = 'Operations Group' THEN NPD_ACT.DEPARTMENT IN ('11', '12', '13')
WHEN @DEPT = 'Security' THEN NPD_ACT.DEPARTMENT IN ('34')
WHEN @DEPT = 'Supply Group' THEN NPD_ACT.DEPARTMENT IN ('50', '98')
WHEN @DEPT = 'Training Group' THEN NPD_ACT.DEPARTMENT IN ('82', '91', '92', '93', '94', '95')
END
返回的错误是
“','附近的语法不正确。”
对于CASE语句的第一行,将返回错误。
答案 0 :(得分:0)
假设@DEPT
仅包含一个部门名称:
您需要在case语句的内部/外部进行切换。
@DEPT = (CASE WHEN NPD.ACT_DEPTARTMENT IN ('40', '43', '45', '46', '47', '83', '84', '85', '86', '87', '89', '90') THEN 'Business Group'
WHEN NPD.ACT_DEPTARTMENT IN ('06', '07') THEN 'Chemistry Group'
WHEN NPD.ACT_DEPTARTMENT IN ('33') THEN 'Emergency Prep'
WHEN NPD.ACT_DEPTARTMENT IN ('60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72') THEN 'Engineering Group'
WHEN NPD.ACT_DEPTARTMENT IN ('21', '22', '23', '24', '25', '26', '76', '78') THEN 'Maintain Group'
WHEN NPD.ACT_DEPTARTMENT IN ('36', '48', '80', '81', '88', '96') THEN 'Management Group'
WHEN NPD.ACT_DEPTARTMENT IN ('11', '12', '13') THEN 'Operations Group'
WHEN NPD.ACT_DEPTARTMENT IN ('54', '73', '79') THEN 'Projects Group'
WHEN NPD.ACT_DEPTARTMENT IN ('34') THEN 'Security'
WHEN NPD.ACT_DEPTARTMENT IN ('50', '98') THEN 'Supply Group'
WHEN NPD.ACT_DEPTARTMENT IN ('82', '91', '92', '93', '94', '95') THEN 'Training Group'
END)
这可以诚实地将翻译表与部门组和部门编号一起使用。