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

SQLServer查询虚字段可以设置类型么?
SQL code
select DICount,(ISNULL(DIClickCount,0)) as ClickCount,(case ISNULL(DICount,0) when 0 then 0 else
ISNULL(DIClickCount,0)/DICount end) as ClickConvert
,ISNULL(DI_EffectCount,0) as EffectCount,(case ISNULL(DI_EffectCount,0) when 0 then 0 else
isNull(DI_EffectCount,0)/DICount end) as EffectConvert from SK_DetailInfo


这里面的ClickConvert是百分比的转换率,
但是,他是Int类型的,当我DIClickCount和DICount 相等 的时候,他是一,如果 DICount 大于DIClickCount的话,就成0了,纠结
查询里面的虚字段可以设置 类型的吗?
或都 , 我这查询 语句怎么改比较好?
还有,在查询里面计算和程序里面计算 ,哪个效率会好点?


------解决方案--------------------
应该还是查询计算快些。 毕竟服务器一般都比客户端计算能力强大。

------解决方案--------------------
SQL code

select DICount,(ISNULL(DIClickCount,0)) as ClickCount,
(case ISNULL(DICount,0) when 0 then 0 else
 1.00*ISNULL(DIClickCount,0)/DICount end) as ClickConvert
,ISNULL(DI_EffectCount,0) as EffectCount,(case ISNULL(DI_EffectCount,0) when 0 then 0 else
isNull(DI_EffectCount,0)/DICount end) as EffectConvert from SK_DetailInfo

------解决方案--------------------
SQL code
cast((case ISNULL(DICount,0) when 0 then 0 else
ISNULL(DIClickCount,0)*1./DICount end) as decimal(8,2)) as ClickConvert