首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请问一个关于case.when语句的困惑

2012-01-31 
请教一个关于case...when语句的困惑?第一条语句,正常:select 供货商,sum(case when isnull(数量,0)0 then

请教一个关于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了,而不是小数了

热点排行