+ -
当前位置:首页 → 问答吧 → SQL求和SUM的扩展问题

SQL求和SUM的扩展问题

时间:2011-11-24

来源:互联网

数据库是mysql。
有一个表,内容如下
type currency count
1 CNY 100
2 CNY 200
1 USD 20
2 USD 50

由于货币单位问题,希望求和时考虑汇率转换。假设换率是USD:CNY为1:5
期望得到结果是:
type sum
1 200 注:200=100+20*5
2 450 注:450=200+50*5  

我这种想法可否实现?
谢谢

作者: netdigger_2000   发布时间: 2011-11-24

select type ,sum(if(currency='USD',5*count,count))
from tt group by type

作者: wwwwb   发布时间: 2011-11-24

select type,sum(case when currency='USD' then count*5 else count end)
from tb
group by type

作者: rucypli   发布时间: 2011-11-24

select type,sum(count*case currency when 'CNY' then 1 when 'USD' then 5 end)
from 有一个表
group by type

作者: ACMAIN_CHM   发布时间: 2011-11-24