日期:2014-05-17 浏览次数:20543 次
USE tempdb GO CREATE TABLE test ( id int , count1 decimal , count2 decimal ) INSERT INTO test SELECT 1, 2 ,3 UNION ALL SELECT 2 ,NULL, 2 UNION ALL SELECT 3, 4, 3 UNION ALL SELECT 4 ,2 ,null SELECT id,count1,count2,CONVERT(decimal(4,2),(ISNULL(count1,0)+ISNULL(count2,0))/ISNULL(count1,1)) FROM test /* id count1 count2 ----------- --------------------------------------- --------------------------------------- --------------------------------------- 1 2 3 2.50 2 NULL 2 2.00 3 4 3 1.75 4 2 NULL 1.00 (4 行受影响) */
------解决方案--------------------
SELECT id,count1,count2,CONVERT(decimal(4,2),(ISNULL(count2,0)-ISNULL(count1,0))/(case when ISNULL(count1,0)=0 then 1 else count1 end)) FROM test
------解决方案--------------------
select count1,count2,(convert(float,count2)-convert(float,count1))/convert(float,count1) increase
from table