日期:2014-05-17  浏览次数:20803 次

聚合函数count里面加条件
count(case(重载里程) when 重载里程>0 then 1 else 0 end) as 重载趟次,
查找的这个字段有误,实在理解错在哪里,请高人指教!
------解决方案--------------------
你的语法错了,要这样
count(case when 重载里程>0 then 1 else 0 end) as 重载趟次

------解决方案--------------------
case when 有两种写法,你的这个写法有误,因为混用了2种写法了。

改成这样试试:

count(case when 重载里程>0 then 1 else 0 end) as 重载趟次
------解决方案--------------------
另外,仔细看了一下,要计算重载趟数,还需要进一步修改一下代码,否则计算出来的趟数就有问题:

sum(case when 重载里程>0 then 1 else 0 end) as 重载趟次

或者这样也行:

count(case when 重载里程>0 then 1 else null end) as 重载趟次
------解决方案--------------------
count才不管记录内容是什么0或1,只是简单的计算记录数,如果你要计算重载趟次,用sum(case when 重载里程>0 then 1 else null end) as 重载趟次,你前面计算出来的是0和1的全部次数