日期:2014-05-18  浏览次数:20351 次

请教一个关于case...when语句的困惑?
第一条语句,正常:
select 供货商,sum(case when isnull(数量,0)=0 then 0 else 1 end)as 品项到货数 from 表1 group by 供货商
可以得出结果:
供货商 品项到货数
0001 1
0002 2
0003 0

第二条语句,正常:
select 供货商,count(商品货号)as 商品总数 from 表1 group by 供货商
可以得到结果:
供货商 商品总数
0001 5
0002 2
0003 0

第三条语句?
select 供货商,sum(case when isnull(数量,0)=0 then 0 else 1 end)/count(商品货号) from 表1 group by 供货商
为什么得到的结果全是0呢?
供货商 未命名
0001 0
0002 0
0003 0

请教高手,这第三条语句的问题出在哪?感谢



------解决方案--------------------
sum(case when isnull(数量,0)=0 then 0 else 1 end)*1.0/count...
------解决方案--------------------
select 1/5
看一下结果是什么就知道怎么回事了!
------解决方案--------------------
select 供货商,sum(case when isnull(数量,0)=0 then 0 else 1 end)*1.0/count(商品货号) from 表1 group by 供货商
------解决方案--------------------
因为SUM之后的数据是INT,而COUNT后的也是INT,
最后的结果当然是int了,而不是小数了