AVG(CASE 报告完成天数 WHEN (委托日期 >= '2004-08-03') AND
(委托日期 <= '2005-01-07') THEN 1 ELSE 0 END) AS [平均完成天期],
==============>
报告完成天数 与 (委托日期 >= '2004-08-03') 类型不匹配
(委托日期 <= '2005-01-07') THEN 1 ELSE 0 END) AS [平均完成天期],
==============>
报告完成天数 与 (委托日期 >= '2004-08-03') 类型不匹配
使用 CASE ... WHEN ... THEN ... 时, WHEN 后边只能是具体的值而非表达式。
1.case input_expression --变量
when when_expression1 then result_expression1
when when_expression2 then result_expression2
...
when when_expressionN then result_expressionN
else else_result_expression
end
叙述,case逐一测试会input_expression和when_expression1到when_expressionN比较,如果和when_expressionK相等,则执行result_expressionK,否则执行else_result_expression(如果没有else_result_expression则不执行)。
2.
case
when Boolean_expression1 then result_expression1
when Boolean_expression2 then result_expression2
...
when Boolean_expressionN then result_expressionN
else else_result_expression
end
case逐一测试Boolean_expression1到Boolean_expressionN,如果Boolean_expressionK为true,则执行result_expressionK,如果都不相同,则执行else_result_expression如果没有else_result_expression则不执行)。所以你的这里:AVG(CASE 报告完成天数 WHEN (委托日期 >= '2004-08-03') AND
(委托日期 <= '2005-01-07') THEN 1 ELSE 0 END) AS [平均完成天期], ---有错误,你只能选择其中一种!没有看到具体的表结构或者数据,不能给出肯定的答复!
avg()的结果受其他栏位的影响,不妨贴出数据看看!
(委托日期 <= '2005-01-07') THEN 1 ELSE 0 END) AS [平均完成天期] 上面那句很乱啊……
直接avg(case when 表达式 then 值1 else 值2 end) as [平均完成天期]
(委托日期 <= '2005-01-07') THEN 1 ELSE 0 END) AS [平均完成天期]
换成:
[平均完成天数]=(select avg(报告完成天数) from xxx where (委托日期 >= '2004-08-03') AND
(委托日期 <= '2005-01-07') and xxx.国家地区=国家地区表.国家名称 )
因为不知道你的表结构是什么,所以就搭建了上面一个结构,不过稍微该一下就应该可以了!
(委托日期 <= '2005-01-07') THEN 报告完成天数 ELSE 0 END) AS [平均完成天期]
(委托日期 <= '2005-01-07') THEN 1 ELSE 0 END) AS [平均完成天期]