日期:2014-05-17  浏览次数:20453 次

SQL查询计算的问题
表tableA
id     cyid    amout
1      100      555
2      101      666 
3      103      777

当cyid表示的值100表示RMB   101表示HKD(汇率0.81)   102表示美元(汇率6.21),请问在对金额进行SUM的时候,将所有的金额转化为RMB呢。。

------最佳解决方案--------------------
select sum( case cyid when 100 then amount*1 when 101 then amount*0.81 when 102 then amount*6.21)
from tb
------其他解决方案--------------------
select sum( case cyid when 100 then amount*1 when 101 then amount*0.81 when 102 then amount*6.21 end )
from tb

或者

select sum( amount*(case cyid when 100 then 1 when 101 then 0.81 when 102 then 6.21 end) )
from tb
------其他解决方案--------------------
二楼的方法不错,顶一下