Halo
发布于 2022-05-13 / 137 阅读 / 0 评论 / 0 点赞

sql 统计历史不同汇率成交

订单表

select * from sales order by date;
datecurrencyval
2022-01-01rmb10.00
2022-01-01hk100.00
2022-02-02rmb100.00
2022-03-01hk10.00
2022-04-02hk100.00
2022-04-02rmb100.00

汇率表

select * from rate order by date;
datecurrencyval
2022-01-01rmb6.30
2022-01-01hk7.20
2022-02-03rmb6.50
2022-02-03hk7.80

求统计记录usd 每日的销售额

select s.date, sum(s.val *(select i.val from rate i where s.date >= i.date and i.currency=s.currency order by i.date desc limit 1)) as usd from sales s group by s.date order by s.date asc;
dateusd
2022-01-01783.0000
2022-02-02630.0000
2022-03-0178.0000
2022-04-021430.0000

评论