我很尴尬地说这是我在SQL Server中的弱点,我无法弄清楚如何让这段代码正常运行。这是一个不同的代码:
SUM(CASE WHEN RRDD Not Like '12%' AND RRDD Not Like '13%' AND RRDD Not Like '16%' AND RRDD
Not Like '17%' AND RRDD Not Like '2706%' AND RRDD Not Like '2707%' AND RRDD Not Like '2331%'
THEN CAST([2011 Total] AS FLOAT)*CASE WHEN IsNumeric [DUR_In_Hours] = 1 THEN
CAST([DUR_IN_Hours] AS FLOAT) ELSE 0 END) AS SP_DOM_COST
我得到的错误如下:
IsNumeric is not recognized and incorrect syntax near DUR_In_Hours
答案 0 :(得分:2)
您遗漏了IsNumeric()
的括号:
WHEN IsNumeric([DUR_In_Hours]) = 1
您还错过了第一个END
上的CASE
:
SUM(CASE
WHEN RRDD Not Like '12%'
AND RRDD Not Like '13%'
AND RRDD Not Like '16%'
AND RRDD Not Like '17%'
AND RRDD Not Like '2706%'
AND RRDD Not Like '2707%'
AND RRDD Not Like '2331%'
THEN CAST([2011 Total] AS FLOAT)
ELSE 0
END
*
CASE
WHEN IsNumeric([DUR_In_Hours]) = 1
THEN CAST([DUR_IN_Hours] AS FLOAT)
ELSE 0
END) AS SP_DOM_COST
最后一个建议,取决于RRDD
字段的数据类型,您可能不需要使用LIKE
语法。您可以将其替换为NOT IN
:
SUM(CASE
WHEN RRDD NOT IN ('12', '13', '16', '17', '2706', '2707', '2331')
THEN CAST([2011 Total] AS FLOAT)
ELSE 0
END
*
CASE
WHEN IsNumeric([DUR_In_Hours]) = 1
THEN CAST([DUR_IN_Hours] AS FLOAT)
ELSE 0
END) AS SP_DOM_COST