我有个订单表,一个汇率表,订单表里有字段 订单号 币种 订单金额 订单日期,汇率表里有币种 汇率 生效日期
汇率表里是一行行的表述每个时段的汇率,比如 2001-2-1 EUR 8.0
2002-2-1 EUR 9.0
... ...我要求订单的总额,订单日期要对应于汇率表的相应时间的汇率
汇率表里是一行行的表述每个时段的汇率,比如 2001-2-1 EUR 8.0
2002-2-1 EUR 9.0
... ...我要求订单的总额,订单日期要对应于汇率表的相应时间的汇率
解决方案 »
- 问一个sql中删除把所有相同行合并为一行的问题,陈良育大哥你看到这个贴的话,就给我回答了吧!!!
- SQL 时间控制事务
- 怎样把Sql语句转化成存储过程语句,急
- 我安装了mysql,安装之后用的还好好的,可是等我再次开机之后,打开数据库,输入密码时就自动退出了,怎么回事啊,求高人解决,谢了谢了!!!
- 这个数据结构应该如何排序
- 在查询分析器里面怎知道表里各字段的类型及长度?
- 求救!sql server 2000新建数据库错误!急!
- 请教一个时间段的查询语句。
- 扩展存储过程中编程调用SQL时的连接问题(高手的境界)
- 关于分区表的创建
- 菜鸟继续送分:未能找到存储过程
- update 触发器 怎么让他不管哪个字段更新了都操作,而不是指定几个被更新后,,,,,
sum(a.订单金额*b.汇率) as 总额
from
订单表 a,汇率表 b
where
a.币种=b.币种 and a.订单日期=b.生效日期
sum(a.订单金额*b.汇率) as 总额
from
订单表 a where not exists(select 1 from 汇率表 b
where a.币种=b.币种 and a.订单日期<b.生效日期 and not exists(select 1 from 汇率表 c where a.币种=c.币种 and b.生效日期>c.生效日期)
select sum(c.订单金额*c.汇率) as 总额
from (select a.币种,a.订单金额,b.汇率,b.生效日期
from 订单表 a,汇率表 b
where a.币种=b.币种 and a.订单日期=b.生效日期)c
where not exists(select 1 from
(select a.币种,a.订单金额,b.汇率,b.生效日期
from 订单表 a,汇率表 b
where a.币种=b.币种 and a.订单日期=b.生效日期)d
where a.币种=d.币种 and c.生效日期<d.生效日期)
from 订单表 a,汇率表b
where
生效日期=(select max( 生效日期) from a , b where 订单日期>生效日期)这个就可以阿
from 订单表 a,汇率表b
where a.币种=b.币种 and b.生效日期=(
select top 1 b.生效日期 from b where ( select * from b where b.生效日期<=a.订单日期 ))
from 订单表 a,汇率表b
where a.币种=b.币种 and b.生效日期=(
select top 1 b.生效日期 from b where ( select * from b where b.生效日期 <=a.订单日期 order by b.生效日期 ))
以下的SQL语句也可以实现你的要求:
select 订单号,订单日期,订单金额,汇率,(订单金额*汇率) as 单笔订单金额 into #all from 订单表 a ,汇率表
where
生效日期=
(select max(生效日期) from 订单表 b , 汇率表
where
订单日期>= 生效日期 and a.订单日期 = b.订单日期 )
and a.币种=汇率表.币种select * from #all
select sum(单笔订单金额) from #all
drop table #all