日期:2014-05-19  浏览次数:20713 次

同比增长率中遇到的问题
select   top   3   t1.单价段   ,   t1.成交数   ,cast(((t1.成交数/t3.总数)*100)   as   nvarchar(16))   +   '% '   as   '比例 ',cast((((t1.成交数-t2.成交数)/t2.成交数)*100)   as   nvarchar(16))   +   '% '   as   '与上月同比 '
from   #table1   t1   inner   join   #table3   t3   on   t1.月份   =   t3.月份
inner   join   #table2   t2   on   t1.单价段   =   t2.单价段
order   by   t1.成交数   desc
--
问题是:现在t2中有的单价段没值而t1.的有值。它就会除0错误。
若判断,那怎么写。怎么算。
请大家指教,在线等。解决即可结贴。

------解决方案--------------------
select top 3 t1.单价段 , t1.成交数 ,cast(((t1.成交数/isnull(t3.总数,1))*100) as nvarchar(16)) + '% ' as '比例 ',cast((((t1.成交数-t2.成交数)/isnull(t2.成交数,1))*100) as nvarchar(16)) + '% ' as '与上月同比 '
from #table1 t1 inner join #table3 t3 on t1.月份 = t3.月份
inner join #table2 t2 on t1.单价段 = t2.单价段
order by t1.成交数 desc

------解决方案--------------------
select top 3
t1.单价段 , t1.成交数 ,
case
when t3.总数 = 0
then NULL
else
cast(((t1.成交数/t3.总数)*100) as nvarchar(16)) + '% '
end as '比例 ',
case
when t2.成交数 = 0
then NULL
else
cast((((t1.成交数-t2.成交数)/t2.成交数)*100) as nvarchar(16)) + '% '
end as '与上月同比 '
from #table1 t1
inner join #table3 t3 on t1.月份 = t3.月份
inner join #table2 t2 on t1.单价段 = t2.单价段
order by t1.成交数 desc