分组后按时间排序每组取最新一条记录


如图,我想是实现按取每一种币别的最新的那条汇率;

 

先按币别分组 筛选出你小于指定日期的最大时间和币别集合

select cur_Id,max(pre_time)as preTime from fc_excrate_adjust fe where fe.pre_time<=to_date('2020-05-11', 'yyyy-MM-dd')  group by cur_Id

在用原表内连接 用条件联立

select a.cur_id,a.old_exchange_rate,a.new_exchange_rate from fc_excrate_adjust a
inner join (select cur_Id,max(pre_time)as preTime from fc_excrate_adjust fe where fe.pre_time<=to_date('2020-05-11', 'yyyy-MM-dd')
group by cur_Id)bs on bs.cur_Id=a.cur_id and bs.preTime = a.pre_time;

这样获取的集合即是每个币种在指定日期前最新汇率的集合了

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM